Sinon错误尝试包装已经包装的函数

Roh*_*are 72 node.js sinon

虽然这里有一个相同的问题,但我找不到我的问题的答案,所以这里提出我的问题:

我正在使用mocha和chai测试我的节点js应用程序.我正在使用sinion来包装我的功能.

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
  });
}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行此测试时,它给了我错误

Attempted to wrap getObj which is already wrapped
Run Code Online (Sandbox Code Playgroud)

我也尝试过

beforeEach(function () {
  sandbox = sinon.sandbox.create();
});

afterEach(function () {
  sandbox.restore();
});
Run Code Online (Sandbox Code Playgroud)

在每个描述中,但仍然给我同样的错误.

zan*_*ngw 94

你应该恢复getObjin after()函数,请尝试如下.

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();
    });
});
Run Code Online (Sandbox Code Playgroud)

  • 有没有办法在不指定每个存根的情况下恢复所有存根?如果有一个 `sinon.restoreAll();` 可以在所有测试之后运行,以确保您不会忘记恢复存根,那就太好了。 (2认同)

Ren*_*ira 10

对于需要恢复一个对象的所有方法的情况,可以使用sinon.restore(obj).

例:

before(() => {
    userRepositoryMock = sinon.stub(userRepository);
});

after(() => {
    sinon.restore(userRepository);
});
Run Code Online (Sandbox Code Playgroud)

  • sinon.restore()在Sinon v2中被弃用,之后被删除.`//以前是sinon.restore(stubObject); // Typescript(stubObject as any).restore(); // Javascript stubObject.restore();` (6认同)

小智 10

此错误是由于未正确恢复存根功能。使用沙箱,然后使用沙箱创建存根。在测试套件还原沙箱之后。

  before(() => {
      sandbox         = sinon.sandbox.create();
      mockObj         = sandbox.stub(testApp, 'getObj', fake_function)
  });
  after(() => {
      sandbox.restore();
  });
Run Code Online (Sandbox Code Playgroud)

  • 我对包装函数进行了多次测试,并且需要使用 **afterEach**。 (2认同)

Amc*_*tty 8

请注意,因为这花了我大约一个小时才弄清楚:

如果您有两个(或更多)测试文件,并且发现无论您尝试什么,仍然beforeEach收到“已包装”错误,请确保您的存根afterEach /替换处理程序位于测试文件的describe块内。

如果将它们放在全局测试范围内,即在describe('my test description', () => {})构造之外,sinon 将尝试两次并抛出此错误。

  • 被严重低估的答案 (2认同)

小智 6

我也使用Mocha的before()和after()钩子来击中它.我也在使用各处提到的restore().单个测试文件运行正常,多个没有.最后找到了关于Mocha根级挂钩:我在我自己的describe()中没有my()和after().因此它在根级别找到所有带有before()的文件,并在开始任何测试之前执行这些文件.

所以请确保您有类似的模式:

describe('my own describe', () => {
  before(() => {
    // setup stub code here
    sinon.stub(myObj, 'myFunc').callsFake(() => {
      return 'bla';
    });
  });
  after(() => {
    myObj.myFunc.restore();
  });
  it('Do some testing now', () => {
    expect(myObj.myFunc()).to.be.equal('bla');
  });
});
Run Code Online (Sandbox Code Playgroud)


Kho*_*hon 5

对于遇到此问题的任何人,如果您对整个对象进行存根或监视,然后您会这样做

沙箱.restore()

您仍然会收到错误。您必须存根/监视各个方法。

我花了很长时间试图找出问题所在。

sinon-7.5.0