chrisk / fakeweb

Ruby test helper for injecting fake responses to web requests
MIT License
1.09k stars 134 forks source link

"FakeWeb.allow_net_connect = true" doesn't work with PORO/ElasticSearch #42

Open BartlomiejSkwira opened 10 years ago

BartlomiejSkwira commented 10 years ago

In my Rails project I have a utility class (plain old ruby object) which does a request to GitHub api and saves Project model. Just a moment ago I have started using ElasticSearch and my Project model has some stuff indexed. Each time I save this model a request to ElasticSearch is made on port 9200. Majority of my tests started to fail (because of the extra requests) with a message:

 FakeWeb::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: PUT http://localhost:9200/karma_tracker_test/project/236

Now when I do

FakeWeb.allow_net_connect = true
#or 
FakeWeb.allow_net_connect = %r[^https?://localhost:9200]

explicitly in a test, or in a before(:each) block in spec_helper the test still fails. But if I put the same line in my utility class just before save, the test passes

Whole code is online at https://github.com/amberbit/KarmaTracker/tree/feature/59079242_elastic_search - failing test spec/model/pivotal_tracker_projects_fetcher_spec.rb:16, failing class app/services/pivotal_tracker_projects_fetcher.rb line 18

Adding a test-related logic inside app logic feels really bad. Any way to make it more clean or is it a bug?