尝试包装已包装的函数时出现SINON错误 [英] Sinon error Attempted to wrap function which is already wrapped

查看:12
本文介绍了尝试包装已包装的函数时出现SINON错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在使用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();
});

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

推荐答案

您应该还原getObjinafter()函数,请尝试如下所示。

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();
    });
});

更新2022/01/22

使用sinon's sanbox您可以使用sandbox.stub()创建存根模拟,并恢复通过sandbox.restore()创建的所有赝品,Arjun Malik给出了一个很好的example

这篇关于尝试包装已包装的函数时出现SINON错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆