I am trying to stub a method using sinon in my Typescript code with Bluebird promises. However, I'm running into an issue where only the first value I set for the stub is being returned, even though I want it to return a different value on the second call.
import sinon = require('sinon')
import * as MyService from "../main/Service"
import * as Promise from "bluebird"
it("test things", function(done) {
let serviceStub = sinon.stub(MyService, 'method')
serviceStub.onFirstCall().returns(Promise.reject("rejected"))
.onSecondCall().returns(Promise.resolve("resolved"))
MyService.method().then(function(value) {
console.log("success 1: "+value.value())
}, function(error) {
console.log("error 1: "+error)
})
MyService.method().then(function(value) {
console.log("success 2: "+value.value())
}, function(error) {
console.log("error 2: "+error)
})
done()
})
I must be doing something wrong with the stubbing since the onSecondCall()
method doesn't seem to work as expected. Instead of returning Promise.resolve("resolved")
, both calls are returning the first value I specified (Promise.reject("rejected")
).
error 1: rejected
error 2: rejected
If the stub worked correctly, it should have output:
error 1: rejected
success 2: resolved
Can anyone help me figure out what's going awry with my stubbing implementation?
Just as a note for those unfamiliar with Bluebird promises, in the method
then(function(value){}, function(error){})
, the first function handles resolved promises and the second function handles rejected promises.