【发布时间】:2014-11-20 08:58:31
【问题描述】:
问题
在我们的代码库中,我们遇到了 sinon 的问题,可以使用下面的代码进行重现。问题是它似乎是间接调用的间谍返回力false,console.log 明确指出该方法已被调用,但spy.called 仍然是false。
代码
以下 CDN 可用于 html:
//cdnjs.cloudflare.com/ajax/libs/sinon.js/1.7.3/sinon-min.js
//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.min.js
main.js
require(['myModule'], function(module) {
//using sinon
var methodCallerSpy = sinon.spy(module, 'methodCaller')
console.log(methodCallerSpy); // methodCaller
module.methodCaller();
console.log(methodCallerSpy.called); //true
var methodSpy = sinon.spy(module, 'method');
console.log(methodSpy); //method
module.methodCaller();
console.log(methodSpy.called); // false
module.method();
console.log(methodSpy.called); // true
});
还有模块
define(function() {
const method = () => console.log('method called by methodCaller');
const methodCaller = () => method();
return{
method,
methodCaller
}
});
【问题讨论】:
标签: javascript sinon