In your case, you are trying to see if bar
was called, so you want to spy bar
rather than foo
.
As described in the doc :
function bar(x,y) {
console.debug(x, y);
}
function foo(z) {
bar(z, z+1);
}
// Spy on the function "bar" of the global object.
var spy = sinon.spy(window, "bar");
// Now, the "bar" function has been replaced by a "Spy" object
// (so this is not necessarily what you want to do)
foo(1);
bar.getCall(0).args => should be [1,2]
Now, spying on the internals of the function strongly couples your test of "foo" to its implementation, so you'll fall into the usual "mockist vs classical" debate.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…