It seems straightforward to create a stub that returns different values on repeated calls: Possible to stub method twice within a single test to return different results?
But, I how does one do this with an async method that resolves on multiple calls?
sinon(module, "myFunction")
.resolves('a')
.resolves('b')
To be clear, in the fragment above, the second resolves overwrites the first, so it always returns 'b'. I'd like the behavior where 'myFunction' first resolves 'a' and then resolves 'b'.
It seems straightforward to create a stub that returns different values on repeated calls: Possible to stub method twice within a single test to return different results?
But, I how does one do this with an async method that resolves on multiple calls?
sinon(module, "myFunction")
.resolves('a')
.resolves('b')
To be clear, in the fragment above, the second resolves overwrites the first, so it always returns 'b'. I'd like the behavior where 'myFunction' first resolves 'a' and then resolves 'b'.
Share Improve this question asked Apr 15, 2022 at 14:28 KurtKurt 5551 gold badge6 silver badges16 bronze badges1 Answer
Reset to default 9Here's the solution:
sinon(module, "myFunction")
.onFirstCall().resolves('a')
.onSecondCall().resolves('b')