Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Sinon错误试图包装已包装的函数_Node.js_Sinon - Fatal编程技术网

Node.js Sinon错误试图包装已包装的函数

Node.js Sinon错误试图包装已包装的函数,node.js,sinon,Node.js,Sinon,虽然这里有一个相同的问题,但我找不到问题的答案,所以我的问题是: 我正在使用mocha和chai测试我的node js应用程序。我正在使用sinion来包装我的函数 describe('App Functions', function(){ let mockObj = sinon.stub(testApp, 'getObj', (dbUrl) => { //some stuff }); it('get results',function(done) { t

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

我正在使用mocha和chai测试我的node 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
  });
}
当我尝试运行此测试时,它会给我错误

Attempted to wrap getObj 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);
});

我还用了摩卡咖啡的before()和after()挂钩。我还使用了restore(),正如到处提到的那样。单个测试文件运行正常,多个测试文件未运行。 最终发现:我自己的descripe()中没有before()和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');
  });
});

建议在“beforeach”中初始化存根,并在“afterEach”中恢复存根。但如果你有冒险精神,下面的方法也可以

describe('App Functions', function(){

  let mockObj = sinon.stub(testApp, 'getObj', (dbUrl) => {
     //some stuff
  });
  it('get results',function(done) {
     testApp.someFun
     mockObj .restore();
  });
}

describe('App Errors', function(){

  let mockObj = sinon.stub(testApp, 'getObj', (dbUrl) => {
     //some stuff
  });
  it('throws errors',function(done) {
     testApp.someFun
     mockObj .restore();
  });
}

此错误是由于未正确还原存根函数造成的。使用沙盒,然后使用沙盒创建存根。在套件中的每个测试之后,恢复沙箱

  beforeEach(() => {
      sandbox = sinon.createSandbox();
      mockObj = sandbox.stub(testApp, 'getObj', fake_function)
  });

  afterEach(() => {
      sandbox.restore();
  });

即使使用沙盒,它也会给您带来错误。特别是当ES6类的测试并行运行时

const sb = sandbox.create();

before(() => {
  sb.stub(MyObj.prototype, 'myFunc').callsFake(() => {
    return 'whatever';
  });
});
after(() => {
  sb.restore();
});
如果另一个测试试图从原型中存根myFunc,则可能引发相同的错误。 我能够解决这个问题,但我并不为此感到骄傲

const sb = sandbox.create();

before(() => {
  MyObj.prototype.myFunc = sb.stub().callsFake(() => {
    return 'whatever';
  });
});
after(() => {
  sb.restore();
});

我和间谍碰上了这件事。这种行为使得sinon在工作中相当不灵活。我创建了一个helper函数,在设置新的spy之前尝试删除任何现有的spy。这样我就不必担心任何前/后状态。类似的方法也可能适用于存根

import sinon,{sinospy}来自'sinon';
/**
*当你在一个方法上设置了一个间谍,而这个方法在以前的测试中已经设置了一个间谍,
*sinon抛出“试图包装已包装的[函数]”错误
*而不是取代现有的间谍。这个helper函数正是这样做的。
*
*@param{object}obj
*@param{string}方法
*/
export const spy=函数spy(对象:T,方法:keyof T):sinospy{
//尝试删除任何现有间谍,以防其存在
试一试{
//@ts忽略
obj[method].restore();
}捕获(e){
//努普
}
返回sinon.spy(对象,方法);

};适用于任何遇到此问题的人,如果您对整个对象进行存根或间谍操作,并且您以后会这样做

sandbox.restore()

你仍然会得到错误。您必须对各个方法进行存根/监视

我浪费了一辈子的时间试图找出问题所在

信农-7.5.0

function stub(obj, method) {
     // try to remove any existing stub in case it exists
      try {
        obj[method].restore();
      } catch (e) {
        // eat it.
      }
      return sinon.stub(obj, method);
    }
并在测试中创建存根时使用此函数。它将解决“Sinon错误试图包装已包装的函数”错误

例如:

stub(Validator.prototype, 'canGeneratePayment').returns(Promise.resolve({ indent: dTruckIndent }));

只是提醒一下,因为我花了大约一个小时才弄明白:

如果您有两个(或更多)测试文件,并且无论您尝试什么,都发现自己仍然出现“已包装”错误,请确保您的
前后
存根/替换处理程序位于测试文件的
描述
块内


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

在对象上存根函数时,这对我不起作用。我必须按照公认的答案对每个函数进行还原。sinon.restore()在sinon v2中被弃用,随后被删除<代码>//以前的sinon.restore(stubObject);//Typescript(stubObject as any).restore();//Javascript stubObject.restore()在尝试了上述公认的方法后,我在“之前”下得到了相同的错误hook@AshwinHegde,你能告诉我你的测试代码吗?也许我可以在这里发现一些问题。没有指定每个存根就无法恢复所有存根吗?如果有一个
sinon.restoreAll()可以在所有测试之后运行,以确保不会忘记恢复存根;你可以在帖子的底部找到一个解释,伙计,救了我的命)这对我有用。我觉得这应该是公认的答案。我有多个包装函数测试,每次测试后都需要使用。在我的情况下,这是正确的答案,因为我监视的是整个对象,而不是特定的方法,所以我无法恢复。