Ruby on Rails:测试第三方 API 调用失败的最佳方法

发布于 2024-12-11 10:39:33 字数 543 浏览 0 评论 0原文

我现在调用第三方网络服务作为我的应用程序的一部分。我正在使用 RestClient gem 来执行此操作。有大量的工具可以完成同样的事情,所以这应该不重要。

我好奇的是拥有足够好的测试,没有什么太花哨的,我可以模拟当第三方 Web 服务因任何原因不可用时我的应用程序如何响应。无论是由于网络延迟/并发症而超出了速率限制还是超时,我只是希望能够获取 HTTP 状态代码之类的内容并测试我的应用程序在该事件中的执行情况。

使用 Test::Unit 执行此操作的最佳方法是什么?现在,对第三方服务的调用封装在我的控制器之一内。我有一个简单的模块,其中包含一些用于远程服务的不同端点的包装方法。我只是想确保我的应用程序在服务可用或不可用时执行正确的操作。

在 Test::Unit 旁边使用一个可以“存根”的附加框架是否是执行此操作的正确方法?显然,我不能强制网络超时,并且仅仅为了测试而开始使用 IPtables 之类的东西进行破解是不值得的。我确信这个问题已经被解决了一百万次,因为如今将 Facebook 和 Twitter 等内容集成到 Web 应用程序中非常流行。当以稳健/受控的格式访问这些 API 时,如何测试失败?

I call a third party web service right now as part of my application. I am using the RestClient gem in order to do this. There are a ton of tools available to do the same thing, so that should not matter.

What I'm curious about is having good enough tests, nothing too fancy, where I can simulate how my application responds when the third party web service is unavailable for whatever reason. Be it that I exceeded a rate limit or a timeout due to network latency/complications, I just want to be able to take something like an HTTP status code and test what my application does in that event.

What's the best way to do this with Test::Unit? Right now the call to the third party service is encapsulated inside of one of my controllers. I have a simple module with some wrapper methods for the different end points of the remote service. I just want to make sure that my application does the right things when the service is or isn't available.

Is using an additional framework next to Test::Unit that can 'stub' the right way to go about doing this? Obviously I can't force a network timeout and starting to hack with things like IPtables just for tests is not worth the time. I'm sure this problem has been solved a million times as integrating things such as Facebook and Twitter into web applications is so popular these days. How do you test for failure when reaching those APIs in a robust/controlled format?

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

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

发布评论

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

评论(2

维持三分热 2024-12-18 10:39:33

我建议使用类似 webmock 的东西来模拟所有你的http请求(不是只是为了模拟失败的请求);它将大大加快您的测试套件的速度,而不必每次运行测试时都实际访问第三方服务。

Webmock 支持 Rest Client 和 Test::Unit。只需将此代码放入您的 test/test_helper.rb 文件中即可:

require 'webmock/test_unit' 

例如,要测试网络超时:

stub_request(:any, 'www.example.net').to_timeout
RestClient.post('www.example.net', 'abc')    # ===> RestClient::RequestTimeout

I would recommend using something like webmock to mock all of your http requests (not just to mock a failed request); it'll greatly speed up your test suite instead of having to actually hit the third party service every time you run the tests.

Webmock supports Rest Client and Test::Unit. Just put this code in your test/test_helper.rb file:

require 'webmock/test_unit' 

As an example, to test a network timeout:

stub_request(:any, 'www.example.net').to_timeout
RestClient.post('www.example.net', 'abc')    # ===> RestClient::RequestTimeout
‖放下 2024-12-18 10:39:33

railscast: 291(仅限订阅者)谈论使用 VCR 和 rspec 进行测试(我知道,不是它不是 Test:Unit)

无论如何你可以考虑使用 VCR 来进行这种 事物

railscast: 291 (subscriber only) talks about testing with VCR and rspec (i know, not it's not Test:Unit)

anyway you could look into using VCR for this sort of thing

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