Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get a simple example to work? #622

Closed
Integralist opened this issue May 25, 2016 · 5 comments
Closed

Can't get a simple example to work? #622

Integralist opened this issue May 25, 2016 · 5 comments

Comments

@Integralist
Copy link

Integralist commented May 25, 2016

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

@bblimke
Copy link
Owner

bblimke commented May 25, 2016

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

@bblimke
Copy link
Owner

bblimke commented May 25, 2016

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

@Integralist
Copy link
Author

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

@bobf
Copy link

bobf commented Sep 29, 2018

@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 !

@bblimke
Copy link
Owner

bblimke commented Oct 7, 2018

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants