Possible to stub method twice within a single test to return different results?

Sinon has a nice API for handling multiple calls (stub.onCall(n);) to the same stubbed method.

Example from stub api doc:

"test should stub method differently on consecutive calls": function () {
    var callback = sinon.stub();
    callback.onCall(0).returns(1);
    callback.onCall(1).returns(2);
    callback.returns(3);

    callback(); // Returns 1
    callback(); // Returns 2
    callback(); // All following calls return 3
}

You can also use onFirstCall() , onSecondCall(), and onThirdCall().

We are using this approach extensively in our tests.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)