How to block external connections with RSpec & Capybara?

In my Rails project, I would like to write tests for non-ideal conditions such as no internet connection or timeouts. For example, I am using a gem to communicate with an API and would like to make sure that I handle the error correctly if there is a connection problem between my application and an external API.

I can do this already by making a mount with a VCR and removing the answer from "cassette". However, this has its drawbacks:

  • This must be done manually.
  • Cassettes cannot be gitignored if I'm working with a team (that's me).

How can I just create a block in my RSpec tests that will discourage external connections, simulating no internet connection?

+3


source to share


2 answers


I have never tried this, but perhaps you could use webmock to mute all requests.

before do 
  stub_request(:any, /.*/).to_return(body: "errors", status: 422)
end

      



More information on how to overlay external services.

+2


source


In this article-thinker

https://robots.thoughtbot.com/how-to-stub-external-services-in-tests#disable-all-remote-connections



# spec/spec_helper.rb
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)

      

+3


source







All Articles