Unable to stub external requests for fake braintree

464 views Asked by At

I have a controller test in which I'm testing a method that also tries to process a payment with braintree. I tried adding fake braintree to the project, but when I run the test I'm told by webmock that I need to stub this request:

Real HTTP connections are disabled. Unregistered request: GET http://127.0.0.1:57793/__identify__ with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'} (WebMock::NetConnectNotAllowedError)

You can stub this request with the following snippet:

stub_request(:get, "http://127.0.0.1:57793/__identify__").
  with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
  to_return(:status => 200, :body => "", :headers => {})

Since the port number varies, I'm using a regex for the url parameter in stub_request:

  stub_request(:get, /http:\/\/127\.0\.0\.1:\d{5}\/__identify__/)
  .with(headers: { 'Accept'=>'*/*',
                   'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
                   'User-Agent'=>'Ruby'})
  .to_return(status: 200, body: '', :headers => {})

Unfortunately, when I run the test I still get the message from webmock saying I need to stub the request.

0

There are 0 answers