I actually cover this exact scenario in my Multitenancy with Rails book.
The difference between your tests and Braintree's project's tests are that your tests are Capybara features and theirs' are controller specs.
I mention this relevant part of Capybara's README within the book:
RackTest is Capybara's default driver. It is written in pure Ruby and does not have any support for executing JavaScript. Since the RackTest driver interacts directly with Rack interfaces, it does not require a server to be started. However, this means that if your application is not a Rack application (Rails, Sinatra and most other Ruby frameworks are Rack applications) then you cannot use this driver. Furthermore, you cannot use the RackTest driver to test a remote application, or to access remote URLs (e.g., redirects to external sites, external APIs, or OAuth services) that your application might interact with.
The way I get around it is that I wrote a gem called fake_braintree_redirect
which inserts a piece of middleware into the request stack during the test environment to capture these requests and respond appropriately. The middleware is added to the stack using an initializer
block defined within application.rb
, like this:
initializer 'middleware.fake_braintree_redirect' do
if Rails.env.test?
require 'fake_braintree_redirect'
config.middleware.use FakeBraintreeRedirect
end
end
This takes Braintree out of the equation completely and returns a successful response whenever you send data to it.
Alternatively, if you really want to test against Braintree's sandbox, you could switch to the JavaScript driver by tagging your scenario
as:
scenario "foo", :js => true