Sinon错误尝试包装已包装的function
虽然这里有同样的问题,但我找不到我的问题的答案,所以这里是我的问题:
我正在testing我的节点js应用程序使用摩卡和柴。 我正在用sinion包装我的function。
describe('App Functions', function(){ let mockObj = sinon.stub(testApp, 'getObj', (dbUrl) => { //some stuff }); it('get results',function(done) { testApp.someFun }); } describe('App Errors', function(){ let mockObj = sinon.stub(testApp, 'getObj', (dbUrl) => { //some stuff }); it('throws errors',function(done) { testApp.someFun }); }
当我尝试运行这个testing时,它给了我错误
Attempted to wrap getDbObj which is already wrapped
我也试过放
beforeEach(function () { sandbox = sinon.sandbox.create(); }); afterEach(function () { sandbox.restore(); });
在每一个描述,但仍然给我同样的错误。
您应该在after()
函数中恢复getObj
,请尝试如下。
describe('App Functions', function(){ var mockObj; before(function () { mockObj = sinon.stub(testApp, 'getObj', () => { console.log('this is sinon test 1111'); }); }); after(function () { testApp.getObj.restore(); // Unwraps the spy }); it('get results',function(done) { testApp.getObj(); }); }); describe('App Errors', function(){ var mockObj; before(function () { mockObj = sinon.stub(testApp, 'getObj', () => { console.log('this is sinon test 1111'); }); }); after( function () { testApp.getObj.restore(); // Unwraps the spy }); it('throws errors',function(done) { testApp.getObj(); }); });
对于需要恢复一个对象的所有方法的情况,可以使用sinon.restore(obj)
。
例:
before(() => { userRepositoryMock = sinon.stub(userRepository); }); after(() => { sinon.restore(userRepository); });