简体   繁体   English

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

[英]Ruby on Rails: Best way to test a failed call to a third party API

I call a third party web service right now as part of my application.我现在调用第三方 Web 服务作为我的应用程序的一部分。 I am using the RestClient gem in order to do this.我正在使用 RestClient gem 来做到这一点。 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.我很好奇的是有足够好的测试,没有什么太花哨的,我可以模拟我的应用程序如何在第三方 Web 服务因任何原因不可用时做出响应。 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.无论是由于网络延迟/复杂性超出了速率限制还是超时,我只想能够使用 HTTP 状态代码之类的东西并测试我的应用程序在该事件中的作用。

What's the best way to do this with Test::Unit?使用 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?是否在 Test::Unit 旁边使用了一个额外的框架,它可以“存根”执行此操作的正确方法? Obviously I can't force a network timeout and starting to hack with things like IPtables just for tests is not worth the time.显然,我不能强制网络超时,并且开始使用 IPtables 之类的东西只是为了测试是不值得的。 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.我确信这个问题已经解决了一百万次,因为如今将 Facebook 和 Twitter 之类的东西集成到 Web 应用程序中是如此流行。 How do you test for failure when reaching those APIs in a robust/controlled format?在以健壮/受控的格式访问这些 API 时,您如何测试失败?

I would recommend using something like webmock to mock all of your http requests (not just to mock a failed request);我建议使用类似webmock 的东西来模拟你所有的 http 请求(不仅仅是模拟失败的请求); 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. Webmock 支持 Rest Client 和 Test::Unit。 Just put this code in your test/test_helper.rb file:只需将此代码放在您的test/test_helper.rb文件中:

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

railscast: 291 (subscriber only) talks about testing with VCR and rspec (i know, not it's not Test:Unit) railscast:291 (仅限订阅者)谈论使用 VCR 和 rspec 进行测试(我知道,这不是 Test:Unit)

anyway you could look into using VCR for this sort of thing无论如何,您可以考虑使用VCR来处理此类事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM