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

Specs not running with elasticsearch #163

Closed
RKushnir opened this issue Feb 15, 2012 · 14 comments
Closed

Specs not running with elasticsearch #163

RKushnir opened this issue Feb 15, 2012 · 14 comments

Comments

@RKushnir
Copy link

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.

@bblimke
Copy link
Owner

bblimke commented Feb 17, 2012

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?

@bblimke bblimke closed this as completed Feb 17, 2012
@bblimke bblimke reopened this Feb 17, 2012
@RKushnir
Copy link
Author

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: karmi/retire#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 :)

@bblimke
Copy link
Owner

bblimke commented Feb 17, 2012

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!

@RKushnir
Copy link
Author

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.

@bblimke
Copy link
Owner

bblimke commented Feb 17, 2012

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.

@RKushnir
Copy link
Author

Nice, thank you!

@aptx4869
Copy link

Just for the record:

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

Workaround:

Use a initializers:

# File: config/initializers/webmock.rb
if Rails.env.test?
  require 'webmock'
  WebMock.disable_net_connect!(allow_localhost: true)
end

@imouaddine
Copy link

thanks @aptx4869

@wellington1993
Copy link

Thanks!

@robertomiranda
Copy link

@aptx4869 👍

@frenkel
Copy link

frenkel commented Jul 29, 2015

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.

@bblimke
Copy link
Owner

bblimke commented Aug 2, 2015

@frenkel thanks for suggestion!

@chase439
Copy link

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?

@chase439
Copy link

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 ;)

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

8 participants