Suppose I have a helper method helper.someFn and service method servMethod which calls helper.someFn multiple times. Now while testing servMethod, I stubbed helper.someFn.
// helper.js
exports.someFn = (param) => {
return param + 1;
}
// service.spec.js
describe('Spec', () => {
it('first test', (done) => {
var someFnStub = sinon.stub(helper, 'someFn', (param) => {
return 0;
});
// do servMethod call which calls someFn
expect(someFnStub.firstCall.calledWith(5)).to.be.true;
helper.someFn.restore();
done();
});
});
Lets say servMethod has called helper.someFn 5 times with different param each time. Inside test, I can access the first-call of helper.someFn with someFnStub.firstCall. I can access till third-call in this way. How can I access next calls like 4th or 5th calls?
The source code shows that firstCall, secondCall and thirdCall are actually just sugar over getCall.
// lib/sinon/spy.js
// ...
this.firstCall = this.getCall(0);
this.secondCall = this.getCall(1);
this.thirdCall = this.getCall(2);
this.lastCall = this.getCall(this.callCount - 1);
// ...
So for asserting on the fourth call you would use stub.getCall(3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With