根据sinon.js的文档,我可以这样做:var sp = sinon.spy(myFunc);
,但它不起作用。这是我的努力:
var sinon = require("sinon");
describe('check bar calling', function(){
it('should call bar once', function() {
var barSpy = sinon.spy(bar);
foo("aaa");
barSpy.restore();
sinon.assert.calledOnce(barSpy);
});
});
function foo(arg) {
console.log("Hello from foo " + arg);
bar(arg);
}
function bar(arg) {
console.log("Hellof from bar " + arg);
}
最佳答案
Sinon 包装了调用,但它并不修补所有引用。返回值是一个包装函数,您可以对其进行断言。它记录对其进行的所有调用,而不是它包装的函数。修改 foo 以便调用者提供一个允许 spy 注入(inject)的函数,并允许对 spy 进行调用。
var sinon = require("sinon");
describe('check bar calling', function(){
it('should call bar once', function() {
var barSpy = sinon.spy(bar);
foo("aaa", barSpy);
barSpy.restore();
sinon.assert.calledOnce(barSpy);
});
});
function foo(arg, barFn) {
console.log("Hello from foo " + arg);
barFn(arg);
}
function bar(arg) {
console.log("Hellof from bar " + arg);
}
关于javascript - 诗农 spy 功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38162229/