最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sinon: how to create a stub that resolves multiple calls - Stack Overflow

programmeradmin5浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 9

Here's the solution:

sinon(module, "myFunction")
  .onFirstCall().resolves('a')
  .onSecondCall().resolves('b')
发布评论

评论列表(0)

  1. 暂无评论