Searchkick: Add section on testing w/ Searchkick

Created on 22 Nov 2013  路  12Comments  路  Source: ankane/searchkick

enhancement help wanted

Most helpful comment

@7h3kk1d I ended up just stubbing out the client, which solved all of the issues with very little code:

RSpec.configure do |config|
  searchkick_client = Searchkick.client

  config.before(:each) do |example|
    if example.metadata.key?(:searchkick)
      Searchkick.client = searchkick_client
    else
      Searchkick.client = SuperStub.new
    end
  end
end

(and SuperStub is just a class that returns self for method_missing)

All 12 comments

Any news on this one @ankane?

Ideally I'd just want to turn off Searchkick alltogether in test (unless when testing specifically this), is there a way to do so? I couldn't seem to find it.

You can call Searchkick.disable_callbacks before your tests.

This seems to improve some things, however we have a lib in which we perform reindexes manually on certain events... it feels a bit silly to check for Rails env at this point. Am I overlooking something?

Sorry if this is not the place to ask, figured since it was about testing it might be okay and give you some extra ideas about how to do this once you get around to it :-)

This place is perfect. In this case, I think you're best bet is to stub out the reindex method.

I'm using the immediate syncing and the disable_callbacks doesn't seem to stop the indexing. Is there a normal way to disable it while running tests?

Calling Searchkick.disable_callbacks should completely disable automatic syncing.

@7h3kk1d I ended up just stubbing out the client, which solved all of the issues with very little code:

RSpec.configure do |config|
  searchkick_client = Searchkick.client

  config.before(:each) do |example|
    if example.metadata.key?(:searchkick)
      Searchkick.client = searchkick_client
    else
      Searchkick.client = SuperStub.new
    end
  end
end

(and SuperStub is just a class that returns self for method_missing)

@evanboho Can you share your SuperStub code? I tried a stub class return self for method_missing but it complains a lot of errors. I don't know if my SuperStub class implementation if correct. Basically I just override the method_missing method to return self. Thanks

@ysyyork I don't have it in front of me anymore, but I think it was as simple as:

class SuperStub
  def method_missing(*args)
    self
  end
end

@evanboho yea I tried this but it failed in some cases. OK, thanks, now I know it should be other issues. I will investigate.

what a wonderful thread. thanks @evanboho

You should take note, however, that there is now a section in the readme for testing this gem with RSpec and you should follow its advice if you are on a newer version (I am using 2.3.2).

Going to close this out. I think we have a pretty good start, and happy to add to it/improve it through additional issues/PRs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

netwire88 picture netwire88  路  3Comments

yurgon picture yurgon  路  4Comments

mehulkar picture mehulkar  路  3Comments

matthewmoss picture matthewmoss  路  4Comments

tomajask picture tomajask  路  4Comments