Testing searchkick with RSpec

人走茶凉 提交于 2019-12-20 10:48:25

问题


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!


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/21814473/testing-searchkick-with-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!