Specs not running with elasticsearch #163

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

Comments

Projects
None yet
8 participants
@RKushnir

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

This comment has been minimized.

Show comment Hide comment
@bblimke

bblimke Feb 17, 2012

Owner

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?

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 Feb 17, 2012

@bblimke bblimke reopened this Feb 17, 2012

@RKushnir

This comment has been minimized.

Show comment Hide comment
@RKushnir

RKushnir Feb 17, 2012

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

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/tire#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

This comment has been minimized.

Show comment Hide comment
@bblimke

bblimke Feb 17, 2012

Owner

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!

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

This comment has been minimized.

Show comment Hide comment
@RKushnir

RKushnir Feb 17, 2012

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.

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

This comment has been minimized.

Show comment Hide comment
@bblimke

bblimke Feb 17, 2012

Owner

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.

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

This comment has been minimized.

Show comment Hide comment
@RKushnir

RKushnir Feb 17, 2012

Nice, thank you!

Nice, thank you!

@RKushnir RKushnir closed this Feb 17, 2012

@aptx4869

This comment has been minimized.

Show comment Hide comment
@aptx4869

aptx4869 Mar 11, 2014

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

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

This comment has been minimized.

Show comment Hide comment
@imouaddine

imouaddine Mar 11, 2014

thanks @aptx4869

thanks @aptx4869

@wellington1993

This comment has been minimized.

Show comment Hide comment
@wellington1993

wellington1993 Jan 16, 2015

Thanks!

Thanks!

@robertomiranda

This comment has been minimized.

Show comment Hide comment
@robertomiranda

robertomiranda Feb 12, 2015

@aptx4869 👍

@frenkel

This comment has been minimized.

Show comment Hide comment
@frenkel

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

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

This comment has been minimized.

Show comment Hide comment
@bblimke

bblimke Aug 2, 2015

Owner

@frenkel thanks for suggestion!

Owner

bblimke commented Aug 2, 2015

@frenkel thanks for suggestion!

@chase439

This comment has been minimized.

Show comment Hide comment
@chase439

chase439 Oct 21, 2015

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?

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

This comment has been minimized.

Show comment Hide comment
@chase439

chase439 Oct 21, 2015

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

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