如何测试外部休息呼叫两次?

发布于 2025-01-31 10:40:13 字数 659 浏览 2 评论 0原文

我正在编写RSPEC测试用例。我想测试用正确的有效载荷两次调用外部端点。但是我似乎找不到解决问题的方法。


client = RealExternalAPI.new 

expect_any_instance_of(HTTP::Client).to(
   receive(:post).and_return(fake_api_response(status: 200))
)

expect_any_instance_of(HTTP::Client).to(
  receive(:post).and_return(fake_api_response(status: 200))
)


client.post_call(payload: 1)
client.post_call(payload: 2) 

这是一个错误消息:


The message 'post' was received by #<HTTP::Client:2282816320 @default_options=#<HTTP::Options:0x0000000110220f40>, @connection=, @state=clean> but has already been received by #<HTTP::Client:0x0000000110223ee8>

I am writing RSpec test cases. I would like to test that an external endpoint has been called twice with the correct payload. But I can't seem to find a solution to my problem.


client = RealExternalAPI.new 

expect_any_instance_of(HTTP::Client).to(
   receive(:post).and_return(fake_api_response(status: 200))
)

expect_any_instance_of(HTTP::Client).to(
  receive(:post).and_return(fake_api_response(status: 200))
)


client.post_call(payload: 1)
client.post_call(payload: 2) 

Here is an error message:


The message 'post' was received by #<HTTP::Client:2282816320 @default_options=#<HTTP::Options:0x0000000110220f40>, @connection=, @state=clean> but has already been received by #<HTTP::Client:0x0000000110223ee8>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隱形的亼 2025-02-07 10:40:13

您可以使用RSPEC消息期望接收计数验证呼叫计数的助手

expect_any_instance_of(HTTP::Client).to(
  receive(:post).twice.and_return(fake_api_response(status: 200))
)

You can use RSpec Message expectations Receive counts helper to verify the call count

expect_any_instance_of(HTTP::Client).to(
  receive(:post).twice.and_return(fake_api_response(status: 200))
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文