Sunday, June 2, 2024
173
rated 0 times [  179] [ 6]  / answers: 1 / hits: 30309  / 7 Years ago, thu, february 23, 2017, 12:00:00

I would like to re-stub someHandler.getStatus, but I'm getting TypeError: Attempted to wrap getStatus which is already wrapped ..



it('is a test', function() {

sandbox.stub(someHandler, 'getStatus', function(callback) {
callback(null, {
value: 1
});
});

sandbox.stub(someOtherHandler, 'doSomething', function(callback) {
callback(null);
});

sandbox.stub(someHandler, 'getStatus', function(callback) {
callback(null, {
value: 0
});
});
});

More From » unit-testing

 Answers
22

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.


[#58811] Tuesday, February 21, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dylondaytond

Total Points: 92
Total Questions: 88
Total Answers: 96

Location: China
Member since Fri, Jan 15, 2021
3 Years ago
dylondaytond questions
Tue, Jun 22, 21, 00:00, 3 Years ago
Thu, May 7, 20, 00:00, 4 Years ago
;