ElasticSearch + Tire: good strategy to mock ES
Asked Answered
T

1

9

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?

Tungstic answered 1/5, 2012 at 20:32 Comment(0)
B
12

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.

Batson answered 2/5, 2012 at 4:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.