Webmock: Can't get a simple example to work?

Created on 25 May 2016  ·  5Comments  ·  Source: bblimke/webmock

Hello,

I'm trying to setup a basic example but seem to be failing to get WebMock to actually stub the GET requests my example is trying to make.

I'm using Mac OS X (10.11.5) with a non-system Ruby version of 2.1.2 (set by rbenv).

My reduced test case looks like the following:

require "webmock"
require "faraday"
require "pry"

endpoint = "http://www.beepbeepbeepboop.com"

WebMock.stub_request(:any, endpoint).to_return(:body => "foobar")

uri = URI.parse(endpoint)
http = Net::HTTP.new(uri.host)
http.get(uri.request_uri)      # SocketError: getaddrinfo: nodename nor servname provided, or not known

f = Faraday.new(nil, nil)
f.get endpoint                 # Faraday::ConnectionFailed: getaddrinfo: nodename nor servname provided, or not known

p "done"

Note: I've tried swapping the order of require "webmock" and require "faraday"
but that made no difference to the outcome

Most helpful comment

require 'webmock' does not enable it since version 2.0.0
You have to add WebMock.enable!

All 5 comments

You have stubbed "http://www.beepbeepbeepboop.com" with foobar response and you make requests to google.

require 'webmock' does not enable it since version 2.0.0
You have to add WebMock.enable!

Aha, there we go WebMock.enable! did the trick 👍 thank you

@bblimke Thanks very much. I'm really confused why this was needed, though.

This test did not pass due to the stub not raising the exception as expected:

        before do
          stub_request(:post, url)
            .with(http_params)
            .to_raise(Faraday::ConnectionFailed)
        end

        it { is_expected.to raise_error(MyCustomError) }

But after adding WebMock.enable! to my rails_helper.rb it worked fine. What confuses me is that I use WebMock all over the place in my tests and it worked just fine there, so is this possibly a bug with WebMock ?

Either way, it's working so I'm happy, but am curious why it needed the explicit call to enable WebMock despite other stubs apparently working fine. Thanks a lot anyway !

@bobf I guess you require 'webmock/rspec' which invokes calls Webmock.enable!

Not sure about your single spec as it can be related to your project. If you are able to extract the code to some sample project to reproduce it, then I can have a look.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rurounijones picture rurounijones  ·  4Comments

vangberg picture vangberg  ·  3Comments

bootstraponline picture bootstraponline  ·  18Comments

lokenmakwana picture lokenmakwana  ·  6Comments

RKushnir picture RKushnir  ·  14Comments