【问题标题】:TypeError: Attempted to wrap which is already wrappedTypeError:尝试包装已经包装的
【发布时间】:2017-08-21 15:59:43
【问题描述】:

我的代码:

  before(function _before() {
    this.myObject = new MyObject();
  });
  it('should', sinon.test(function() {
    const stubLog = sinon.stub(this.myObject.log, 'warn');
  }));
  it('should 2', sinon.test(function() {
    const stubLog = sinon.stub(this.myObject.log, 'warn');
  }));

sino版本:1.17.6

为什么我在应该 2 测试中收到错误 TypeError: Attempted to wrap warn which is already wrapped?我应该手动恢复stubLog 吗?我认为sinon.test() 会为我做这件事。也许我做错了什么?

欢迎任何 cmets。谢谢

【问题讨论】:

    标签: javascript mocha.js sinon


    【解决方案1】:

    您是否尝试过在沙盒中使用this.stub() 而不是sinon.stub()

    const stubLog = this.stub(this.myObject.log, 'warn');
    

    来自official docs,一个例子:

    it('should do something', sinonTest(function(){
        var spy1 = this.spy(myFunc);
        var spy2 = this.spy(myOtherFunc);
        myFunc(1);
        myFunc(2);
        assert(spy1.calledWith(1));
        assert(spy1.calledWith(2));
    }));
    

    一旦使用sinon.test() 包装,该函数将访问自身内部的this 指针并使用它来存根/监视其他函数。

    这样使用它应该会自动为您恢复存根。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-04
      • 2013-06-21
      • 2019-09-22
      • 2012-02-08
      • 2015-07-13
      • 2023-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多