Skip to content
This repository has been archived by the owner on Jun 30, 2018. It is now read-only.

Connects to search server on application boot - problem with WebMock #136

Closed
ramontayag opened this issue Oct 27, 2011 · 7 comments
Closed

Comments

@ramontayag
Copy link

Hey @karmi,

I upgraded to 0.3.8 today and noticed that my tests started to fail. I didn't add other gems. This is the error I got:

/Users/dev/.rvm/gems/ree-1.8.7-2011.03@mrx/gems/webmock-1.7.7/lib/webmock/http_lib_adapters/net_http.rb:100:in `request': Real HTTP connections are disabled. Unregistered request: HEAD http://localhost:9200/test with headers {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate'} (WebMock::NetConnectNotAllowedError)

I use WebMock because that's one of the two gems required by vcr. After some playing around, I was able to determine that my tests started to fail because the newer version of Tire that I installed seems to connect to the search server when the application boots (that's the require environment.rb line in the spec_helper).

My spec_helper used to look like this:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
# ...snip

Now, I have to set it like this to avoid the error:

ENV["RAILS_ENV"] ||= 'test'

# Set webmock, that is used by vcr, to allow net connections BEFORE we require the environment file
require 'webmock'
WebMock.allow_net_connect! # or whatever method you want WebMock to allow connections

require File.expand_path("../../config/environment", __FILE__)

I know this isn't a tire-specific problem, but I thought it was worth mentioning. It took me a while to find (I should read errors more closely), but you may want to put a notice in the README or some automatic detection or something.

@karmi
Copy link
Owner

karmi commented Oct 28, 2011

Hi, yeah, Tire models check with the ES server if the corresponding index exists or not, so they know if they should create it or not. You should mock the HEAD request with WebMock/FakeWeb, etc.

Do you have any specific tip where & how to mention it in the Readme?

@karmi
Copy link
Owner

karmi commented Nov 17, 2011

Hi, thanks, I'll close this for now, and will keep the note about instructing users to mock ES requests in their unit tests in mind...

@karmi karmi closed this as completed Nov 17, 2011
@RKushnir
Copy link

I'm having the same issue. Used the trick with allow_net_connect! but it doesn't feel quite right.

@karmi
Copy link
Owner

karmi commented Feb 16, 2012

@RKushnir which "trick" and why doesn't it feel "quite right"?

@RKushnir
Copy link

@karmi From the first post:

require 'webmock'
WebMock.allow_net_connect!

I think, this is rather the issue of webmock, it should allow to configure when to turn it on. Maybe, start as disabled by default.

@karmi
Copy link
Owner

karmi commented Feb 16, 2012

Yeah, Fakeweb has a reversed approach, by default all requests are allowed. Maybe the code in test helpers could be structured so that Webmock and it's definition are loaded first, and Tire is loaded afterwards? We require 'tire' in our test helper after all initialization has been done IMHO.

@sirn
Copy link

sirn commented May 8, 2013

Sorry for replying to a 1 year old issue. However I've found this to work as well:

require 'webmock'
WebMock.disable_net_connect!(:allow_localhost => true)

I think this way is better than allow_net_connect! in that it still blocks external connections while also allows localhost connection. (Which might make sense if ElasticSearch is running on localhost for testing.)

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

No branches or pull requests

4 participants