简体   繁体   中英

Testing Network Timeouts and Errors with Ruby

I'm writing a wrapper for a flakey 3rd party API and would like some way to test my code's error handling.

What do people do when they want to test things like:

  • API responding with various http codes (400-599)
  • Connection timeouts (host is down, dns issues, etc)

Is there a correct way to do this? Right now i'm simply using a test host, adding mod_rewrite rules such as:

RewriteRule ^500.html$ / [R=500,L,NE]
RewriteRule ^501.html$ / [R=501,L,NE]
RewriteRule ^404.html$ / [R=404,L,NE]

I then make sure my code handles those use cases appropriately but it feels wrong and provides no way to test a timeout error.

If you're going to be testing routes like that, I would highly suggest a combination of rspec and capybara. The rspec framework gives a lot of flexibility, and the high user base provides a ton of places for you to get started.

我建议您使用mocha(rSpec中的默认值),rr,flexmock等模拟/存根api请求。还有一些特殊的模拟库可用于处理HTTP请求,但是我不确定是否可以模拟整个响应或仅模拟主体。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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