Zachary Forster
Zachary Forster

Reputation: 119

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

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...

Upvotes: 8

Views: 3236

Answers (1)

mantoni
mantoni

Reputation: 1622

A 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"

Upvotes: 8

Related Questions