使用RSpec测试searchkick
我想在我的练习管理应用程序中创建用于搜索患者的function规范。
到目前为止,我已经在网上搜索并遵循以下建议的解决方案:
http://bitsandbit.es/post/11295134047/unit-testing-with-tire-and-elastic-search#disqus_thread
和
https://github.com/karmi/tire/wiki/Integration-Testing-Rails-Models-with-Tire
这两篇文章都建议为ElasticSearch和Tire配置spec_helper.rb。 由于Searchkick基于Tire,我将解决方案应用于Patient类,这是我使用Searchkick的唯一模型。
但是,我得到每个配置的’NoMEthodError’。 例如,使用以下代码:
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
我收到以下错误:
Failure/Error: Unable to find matching line from backtrace NoMethodError: undefined method `index_name'
方法’index’和’create_elasticsearch_index’也是如此
我对RoR很新,老实说我不确定我在这里做错了什么,除了假设我可以在Searchkick上使用Tire解决方案。 所以任何帮助都非常感谢!
Searchkick需要更好的测试文档,但这里有它的要点:
Searchkick会在每个环境中自动使用不同的索引名称,因此无需进行任何设置。 在控制台中运行Patient.searchkick_index.name
以确认这一点。
您可以直接调用reindex
,而不是删除和重新创建reindex
。
RSpec.configure do |config| do config.before :each do Patient.reindex end end
最后,在插入数据后,在调用Patient.searchkick_index.refresh
之前调用Patient.search
。 这告诉Elasticsearch立即更新索引(而不是刷新间隔后,默认为1秒)。
即使Searchkick基于Tire,也不意味着您的模型上可以使用Tire方法。 有关可用方法的文档,请参阅https://github.com/ankane/searchkick 。 有关使用Searchkick和使用Tire之间的对比,请参阅https://github.com/ankane/searchkick#migrating-from-tire小节。