Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinon: how to create a stub that resolves multiple calls

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'.

like image 433
Kurt Avatar asked Sep 16 '25 22:09

Kurt


1 Answers

Here's the solution:

sinon(module, "myFunction")
  .onFirstCall().resolves('a')
  .onSecondCall().resolves('b')
like image 176
Kurt Avatar answered Sep 19 '25 08:09

Kurt