Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 413
Zachary Forster Avatar asked Mar 16 '15 19:03

Zachary Forster


1 Answers

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"
like image 150
mantoni Avatar answered Sep 28 '22 06:09

mantoni