Question

I'm trying to stub any external API calls in my test suite, but the before(:suite) is never executed. Webmock always reports that I need to stub the maps.googleapis.com even though no tests have been run yet (no green dots, no red Fs).

spec_helper.rb:

require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)

...

config.before(:suite) do
    puts "THIS NEVER SHOWS"
    stub_request(:get, "maps.googleapis.com").
      with(headers: {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
      to_return(status: 200, body: "", headers: {})
end

The geocoder gem ends up trying to save the lat/lon from googleapis.com and an error is raised by Webmock saying that the URL is unregistered.

EDIT: Error snippet:

$ bundle exec rspec spec/factories_spec.rb
/home/jake/.rvm/gems/ruby-2.1.0@global/gems/webmock-1.17.4/lib/webmock/http_lib_adapters/net_http.rb:114:in `request': Real HTTP connections are disabled. Unregistered request: GET http://maps.googleapis.com/maps/api/geocode/json?address=[private]&language=en&sensor=false 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://maps.googleapis.com/maps/api/geocode/json?address=[private]&language=en&sensor=false").
  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 => {})

============================================================
    from /home/jake/.rvm/gems/ruby-2.1.0@global/gems/geocoder-1.1.9...

    ...

Again, I'll stress that this has to do with the fact that the code in the config.before(:each) block is never run. Why? Because if it was, I could "raise 'WTF'" and 'WTF' should appear in the console output instead of the error you see above. I only see 'WTF' when I "un-bundle" the Webmock gem.

Was it helpful?

Solution

Well I was doing "something cute" with my RSpec tests by creating tests at runtime depending on whether or not the Factory has an attribute that is a file. Due to the way my factories/models were set up, factories were being created (saved) when the attributes for a certain factory were being read, so the block of code that's generating the tests runs outside of RSpec's config.before(:suite) and WebMock raises the error.

https://github.com/bblimke/webmock/issues/378

Moreover, here's specifically what I was doing wrong - not related to WebMock:

1) In my factories.rb, I was calling create() for associations which may not yet exist. Why? Because RSpec was giving me errors saying "[association] was blank". It was doing that because I had validates_presence_of :association_id instead of just :association. When I used create() instead of build(), it "worked". Of course when it came time to use WebMock, I was creating (and thus saving) objects calling geocoder to do it's thing. The solution was to fix validates_presence_of to use the right attribute and use build() instead of create() in my factories.

Bad Example:

# In spec/factories.rb
factory :review, class: Manager::Review do
    rating 4
    wine { Manager::Wine.first || create(:wine) }
    reviewer { Manager::Reviewer.first || create(:reviewer) }
    date Time.now
    association :referral, referrable_id: 1, referrable_type: Manager::Review, strategy: :build
end

# In app/models/manager/review.rb
validates_presence_of :rating_id, :wine_id, :reviewer_id, :date

Good Example:

# In spec/factories.rb
factory :review, class: Manager::Review do
    rating 4
    wine { Manager::Wine.first || build(:wine) }
    reviewer { Manager::Reviewer.first || build(:reviewer) }
    date Time.now
    association :referral, referrable_id: 1, referrable_type: Manager::Review, strategy: :build
end

# In app/models/manager/review.rb
validates_presence_of :rating, :wine, :reviewer, :date

2) FWIW, I told geocoder to fetch the geocode before_save, not after_validate like it suggests in their home page.

Also, you cannot stub with WebMock in the before(:suite), but it works in before(:each)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top