简体   繁体   中英

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!

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).

Even if Searchkick was based on Tire, that doesn't mean that the Tire methods are available on your models. See https://github.com/ankane/searchkick for documentation on what methods are available. See the subsection https://github.com/ankane/searchkick#migrating-from-tire in particular for a contrast between using Searchkick and using Tire.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM