2

I use Faraday gem in app, and I want to test how my app works when Faraday connection fails by timeout. How can I stub request for this case?

pustserg
  • 141
  • 1
  • 5
  • 2
    `allow(conn).to receive(:get).and_raise(Faraday::TimeoutError)` see http://stackoverflow.com/questions/9008442/how-do-i-test-rspec-a-http-request-that-takes-too-long – max Jun 15 '15 at 18:45
  • thanks, I've seen it, but my app catches this error, and works with it. It cannon raise exception, I want to test how it works. – pustserg Jun 15 '15 at 19:03
  • Maybe setup a firewall rule that will drop packets (silently discard them) to the destination host. I think that'll cause Faraday connection to that host to timeout. – egwspiti Jun 15 '15 at 19:12
  • You could use webmock http://stackoverflow.com/questions/25552239/webmock-simulate-failing-api-no-internet-timeout – max Jun 15 '15 at 19:13
  • But I'm a bit confused; if you application swallows the error how do you actually expect to test different outcomes? – max Jun 15 '15 at 19:17
  • @maxcal: ```rescue Faraday::ClientError => e``` and write fail_reason to db as e.code – pustserg Jun 15 '15 at 19:23

1 Answers1

2

I found a working method. May be it is not beautiful, but is works.

faraday_adapter.post('fail') do
  raise Faraday::TimeoutError
end
pustserg
  • 141
  • 1
  • 5