Testing Search with RSpec

I would like to create function specifications for finding Patients in my practice management application.

So far I have searched the network and followed the proposed solutions:

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 spec_helper.rb configurations for ElasticSearch and Tire. Since Searchkick was based on Tire, I applied solutions for the Patient class, which is the only model in which I use Searchkick.

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 thing happens with the "index" and "create_elasticsearch_index" methods

I am new to RoR, and to be honest, I'm not sure what I can do wrong here, except maybe on the assumption that I can use Tire solutions on Searchkick. Therefore, any help is greatly appreciated!

+6
source share
2 answers

Searchkick needs better test documentation, but here’s the point:

Searchkick automatically uses a different index name in each environment, so there is no need to make any settings. Run Patient.searchkick_index.name in the console to confirm this.

Instead of deleting and re-creating the index, you can simply call reindex .

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

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

+24
source

Even if Searchkick was based on Tire, this does not mean that Tire methods are available on your models. See https://github.com/ankane/searchkick for documentation on which methods are available. See Subsection https://github.com/ankane/searchkick#migrating-from-tire , in particular, for the contrast between using Searchkick and using Tire.

0
source

All Articles