Sinon间谍function不起作用

根据sinon.js的文档,我可以这样做: var spy = 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包装了这个电话,并没有补充所有的参考文献。 返回值是一个封装函数,您可以在其上进行断言。 它logging所有对它的调用,而不是它所包装的function。 修改foo使调用者提供一个函数允许间谍被注入,并允许调用间谍。

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