所以我正在使用sinon stubs编写javascript单元测试。在代码中调用的stubbed方法包含在延迟(underscore delay)中。但是,实际的方法是调用而不是存根。当我不使用延迟时,它按预期工作并调用存根。我在下面列出了代码的示例。知道可能会发生什么吗?我得到的一个假设是,sinon正在跟踪someObject的引用以恢复存根,并且由于我使用了延迟,someObject超出了范围,并且存根被恢复为实际方法。
// This is code form the unit test.
// someObject.someFunction is stubbed
sinon.stub(someObject, 'someFunction', function() {
console.log('stubbed someFunction');
});
someObject.execute();
// These methods are defined within someObject
execute: function() {
_.delay(
function() {
this.someFunction();
},
1000
);
}
// This is called from the unit test despite being stubbed.
// However, when the _.delay() is removed, the stubbed
// method is called.
someFunction: function() {
console.log('real someFunction');
}