Seriously, I can't figure this out. The documentation gives us:
stub.callsArg(index) - Causes the stub to call the argument at the provided index as a callback function. stub.callsArg(0); causes the stub to call the first argument as a callback.
However, I've got no idea where this list of arguments to be indexed into is. Maybe I just don't understand what a stub is...
Seriously, I can't figure this out. The documentation gives us:
stub.callsArg(index) - Causes the stub to call the argument at the provided index as a callback function. stub.callsArg(0); causes the stub to call the first argument as a callback.
However, I've got no idea where this list of arguments to be indexed into is. Maybe I just don't understand what a stub is...
Share Improve this question asked Mar 16, 2015 at 19:15 Zachary ForsterZachary Forster 1191 silver badge4 bronze badges1 Answer
Reset to default 8A stub is a noop function with programmable behavior. In your case callsArg(index)
will program the stub to expect a function at index
and immediately invoke it.
function sayHi() {
console.log('hi');
}
var stub = sinon.stub().callsArg(2);
stub('abc', 42, sayHi); // prints "hi"