我的代碼:摩卡測試中sinon.test()中的this是什麼?
it('should', sinon.test(function() {
console.log(this); // what `this` refer here?
...
}));
在上面的代碼,什麼this
指在sinon.test()
功能?
我試圖登錄,但是得到了錯誤:
TypeError: Converting circular structure to JSON
興農版本:1.17.6
歡迎任何意見。謝謝。
UPDATE
閱讀下面的答案後,我仍然感到困惑。爲什麼當this.myOnject.log
僅存在一次時,以下兩段代碼有效?
it('should', sinon.test(function() {
const stubLog = this.stub(this.myObject.log, 'warn');
// ...
this.myObject.process();
// expect codes...
}));
it('should', sinon.test(function() {
const stubLog = sinon.stub(this.myObject.log, 'warn');
// ...
this.myObject.process();
// expect codes...
}));
UPDATE
如果downvoted,請留下一些評論,讓我知道你爲什麼downvoted。我對發佈的問題感到非常困惑。但我更爲困惑,爲什麼這麼多人低調,但沒有留下任何有用的評論。
謝謝。 'sinon.test()'中的'sinon.stub()'和'this.stub()'有什麼區別? – BAE
區別在於'sinon.test()'中的'this.stub()'使用沙箱。如果你只是調用'sinon.stub()',你不會從sanbox中受益,那麼你必須自己執行任何必要的清理。 – Louis