Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing searchkick with RSpec

I would like to create feature specs for searching Patients in my Practice Management app.

So far, I have searched the net and have followed suggested solutions from:

http://bitsandbit.es/post/11295134047/unit-testing-with-tire-and-elastic-search#disqus_thread

and

https://github.com/karmi/tire/wiki/Integration-Testing-Rails-Models-with-Tire

Both of these articles suggested configurations to spec_helper.rb for ElasticSearch and Tire. Since Searchkick was based on Tire, I applied the solutions to the class Patient, which is the only model I am using Searchkick on.

However, I get a 'NoMEthodError' for each of the configurations. For example, using the following code:

spec_helper.rb

RSpec.configure do |config| do
  .
  .
  .

  config.before :each do
    Patient.index.delete
    Patient.create_elasticsearch_index
  end

  config.before :all do
   Patient.index_name('test' + Patient.model_name.plural)
  end
end

I get the following error:

Failure/Error: Unable to find matching line from backtrace
 NoMethodError:
   undefined method `index_name' 

The same happens to the methods 'index' and 'create_elasticsearch_index'

I am fairly new to RoR and am honestly not sure what I could be doing wrong here, except maybe for assuming I could use Tire solutions on Searchkick. So any help is very much appreciated!

like image 929
mfamador Avatar asked Feb 16 '14 17:02

mfamador


1 Answers

Searchkick needs better testing documentation, but here's the gist of it:

Searchkick automatically uses a different index name in each environment, so no need to do any set up. Run Patient.searchkick_index.name in the console to confirm this.

Instead of deleting and recreating the index, you can just call reindex.

RSpec.configure do |config| do
  config.before :each do
    Patient.reindex
  end
end

Finally, after inserting data, call Patient.searchkick_index.refresh before calling Patient.search. This tells Elasticsearch to update the index immediately (rather than after the refresh interval, which defaults to 1 second).

like image 173
Andrew Kane Avatar answered Nov 11 '22 22:11

Andrew Kane