I use ElasticSearch on the homepage of my site. In my acceptance tests, when a user logs in, he's redirected to the homepage.
But using ES in a test is expensive (need to create and delete the index), so I don't want to have to do that every time a user goes through the homepage.
I would like to do the actual ES search only when the test has some metadata:
config.before(:each) do
if example.metadata[:elastic]
Model.create_elasticsearch_index
end
end
scenario "Results should be ordered by distance", :elastic do
# tests...
end
So I would need to "mock" the search and not use ES when the test does no have the :elastic metadata.
What would be a good way to achieve that?
I would probably use FakeWeb to selectively enable and disable live HTTP calls.
To mock calls to ES:
FakeWeb.allow_net_connect = false
FakeWeb.register_uri(:any, %r|\Ahttp://localhost:9200|, :body => "{}")
To allow calls to ES:
FakeWeb.clean_registry
FakeWeb.allow_net_connect = true
Allowing and disallowing net connections isn't strictly required here, since FakeWeb's mocks get priority over real calls, but I find that it helps to throw an exception in your tests when something makes an unmocked HTTP call.
You can probably expand on this to use the test metadata to enable or disable mocks as needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With