使用 RSpec 测试 searchkick [英] 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
和
https://github.com/karmi/轮胎/wiki/Integration-Testing-Rails-Models-with-Tire
这两篇文章都建议了对 ElasticSearch 和 Tyre 的 spec_helper.rb 的配置.由于 Searchkick 基于 Tyre,我将解决方案应用于 Patient 类,这是我使用 Searchkick 的唯一模型.
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.
但是,对于每个配置,我都会收到NoMEthodError".例如,使用以下代码:
However, I get a 'NoMEthodError' for each of the configurations. For example, using the following code:
spec_helper.rb
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' 方法也是如此
The same happens to the methods 'index' and 'create_elasticsearch_index'
我对 RoR 还很陌生,老实说,我不确定我在这里做错了什么,除了假设我可以在 Searchkick 上使用 Tyre 解决方案.所以非常感谢任何帮助!
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 需要更好的测试文档,但它的要点如下:
Searchkick needs better testing documentation, but here's the gist of it:
Searchkick 会在每个环境中自动使用不同的索引名称,因此无需进行任何设置.在控制台中运行 Patient.searchkick_index.name
以确认这一点.
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.
您可以调用 reindex
而不是删除和重新创建索引.
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
最后,插入数据后,先调用Patient.searchkick_index.refresh
,再调用Patient.search
.这告诉 Elasticsearch 立即更新索引(而不是在刷新间隔之后,默认为 1 秒).
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).
这篇关于使用 RSpec 测试 searchkick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!