I'm using webmock 1.7.10 and elasticsearch+tire(https://github.com/karmi/tire). When running the specs it tries to access localhost:9200 on start and webmock halts the process because this request is not stubbed. I tried WebMock.disable!
in spec_helper.rb
right after require 'webmock/rspec'
but this didn't help. Then tried stubbing that URL but it didn't help either.
WebMock.disable_net_connect!(:allow_localhost => true)
and
WebMock.allow_net_connect!
also didn't work.
Is there any solution or workaround? This is not specific to some particular specs, this prevents me from being able to run specs at all, so it's a major issue for me, please advice.
Is it possible that a separate process is started, with a separate Webmock instance loaded, rather the one configured in the spec helper?
Are you able to create a minimum code snippet to reproduce this issue?
Here are just the bare bones:
# Gemfile
# ...default Rails gemset here...
gem 'tire'
group :test, :development do
gem "rspec-rails", "~> 2.8"
end
group :test do
gem 'webmock'
end
# rails g model Item name:string
# models/item.rb
class Item < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :name, :analyzer => 'snowball', :boost => 100
end
end
Everything else is standard Rails stuff. When running rake
I get
/webmock-1.7.10/lib/webmock/http_lib_adapters/net_http.rb:99:in `request_with_webmock': Real HTTP connections are disabled. Unregistered request: HEAD http://localhost:9200/items with headers {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'} (WebMock::NetConnectNotAllowedError)
There's a similar issue on tire
page: https://github.com/karmi/tire/issues/136
As I've written there I solved this by inserting
require 'webmock'
WebMock.allow_net_connect!
at the very top of spec_helper.rb
.
I'm not sure to which of the two gems this belongs more :)
That's correct behaviour then. WebMock by default blocks all requests unless you tell him not to do it, by using disable! or allow_net_connect!
But is the way that I used the 'right' way to do it? Or maybe there are any configuration hooks to put this in? I'm glad that I somehow got it running, but still concerned about whether it's correct.
Yes, that's a correct way.
It's arguable whether webmock should disable all connections by default. I like this feature, but not everyone does.
I.e it's very common to disable_net_connect with exception to localhost when webmock is used with selenium webdriver.
If you want to be more specific, and allow only one connection, you could do WebMock.disable_net_connect!(:allow => "localhost:9200") instead.
Nice, thank you!
Adding
require 'webmock'
WebMock.allow_net_connect!
to very top of spec_helper.rb
doesn't work when you use the spring gem which will be enabled by default in rails 4.1 to preload rspec
It will raise a WebMock::NetConnectNotAllowedError
and break the test suit
Use a initializers:
# File: config/initializers/webmock.rb
if Rails.env.test?
require 'webmock'
WebMock.disable_net_connect!(allow_localhost: true)
end
thanks @aptx4869
Thanks!
@aptx4869 :+1:
IMHO it is better to add require: false
to the gem 'webmock'
line in Bundler, then load webmock with a require in your test_helper and right after that disable it for localhost.
@frenkel thanks for suggestion!
Thank you for your work on the gem.
I've been getting sporadic corruption and failures in my rspec test suite. Similar to the discussion in this thread, I have gem 'webmock', require: false
in my Gemfile. Basically, my question is How do I enable real requests by default for the case I have below?
I have a test file1 (for testing fake requests) that require 'webmock/rspec'
and cleans up after itself by stubbing request and WebMock.allow_net_connect!
after all its tests complete. Also, I have a test file2 (for testing real requests) that has no require 'webmock/rspec'
.
I realized that if file2 runs first, then its real requests would fail. I believe this is because rspec loads all the test files and its require statements, which in my case a require 'webmock/rspec'
in any files would disable all real requests across the suite. For many test files that use real requests, I don't want to put WebMock.allow_net_connect!
in each of those files when they don't really need to know about webmock.
The suggestion to add the following to the top of spec_helper.rb:
require 'webmock'
WebMock.allow_net_connect!
kind of work, but breaks my spork (yes, it's kinda old technology) with cryptic errors. My question: Is there another way?
I overlooked it, but the suggestion by @aptx4869 to add config/initializers/webmock.rb
solved my spork issue and allowed real requests by default. Hopefully my use case above can add a point to enable real connections by default ;)
Most helpful comment
Just for the record:
Adding
to very top of
spec_helper.rb
doesn't work when you use the spring gem which will be enabled by default in rails 4.1 to preload rspecIt will raise a
WebMock::NetConnectNotAllowedError
and break the test suitWorkaround:
Use a initializers: