What does stub.callsArg (index) do from Sinon.JS?

Seriously, I can't figure it out. The documentation gives us:

stub.callsArg (index) . Causes the stub to call an argument at the specified index as a callback function. stub.callsArg (0); causes the stub to call the first argument as a callback.

However, I have no idea where this list of arguments to be indexed is. Maybe I just don’t understand what a stub is ...

+4
source share
1 answer

Pieces are a noop function with programmable behavior. In your case, it will callsArg(index)program the stub to wait for function in indexand immediately call it.

function sayHi() {
  console.log('hi');
}
var stub = sinon.stub().callsArg(2);
stub('abc', 42, sayHi); // prints "hi"
+6

All Articles