Friday, 7 April 2017

Is there a way to tell per call what a stub was called with in Sinon?

I have a function that deals with paging. As a result, I want to make sure that successive calls to the function are good. I know I can stub the functions as so:

const firstResponse = {...}
const secondResponse = {...}

let myStub = sinon.stub();

myStub.onFirstCall().returns(firstResponse);
myStub.onSecondCall().returns(secondResponse);

However going the other direction does not work:

// Call my function here...
const calledFirstCorrectly = myStub.onFirstCall().calledWith({page_one_args});
const calledSecondCorrectly = myStub.onSecondCall().calledWith({page_two_args});

I get

myStub.onFirstCall().calledWith(...) is not a function

Which is consistent with the Sinon documentation. However, digging through the stub documentation doesn't reveal an obvious solution to my problem.

Is there a way to do this?



via rec