2017-08-21 82 views
0

我的代碼:類型錯誤:試圖包裹這已經是包裹

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

興農版本:1.17.6

爲什麼我應在2測試得到了錯誤TypeError: Attempted to wrap warn which is already wrapped?我應該手動恢復stubLog嗎?我認爲sinon.test()會爲我做。也許我做錯了什麼?

歡迎任何評論。謝謝

回答

0

您是否嘗試過在沙箱內使用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指針內部本身並用它來存根/間諜其它功能。

像這樣使用它應該爲你自動恢復你的存根。