Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check nth call in Sinon stubbed method

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?

like image 420
Tanvir Hossain Bhuiyan Avatar asked Dec 19 '22 04:12

Tanvir Hossain Bhuiyan


1 Answers

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)

like image 126
GeriTol Avatar answered Dec 22 '22 01:12

GeriTol