Rob*_*bin 43 rspec ruby-on-rails ruby-on-rails-3 elasticsearch tire
我想知道在使用ElasticSearch和Tire时如何在应用程序中测试搜索.
您如何设置新的ElasticSearch测试实例?有没有办法嘲笑它?
你知道的任何宝石可能对此有所帮助吗?
我发现一些有用的东西:
我发现了一篇很棒的文章回答了我所有的问题:)
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
在问之前我无法相信我没有找到这些......
spa*_*njo 34
您可以为每个环境设置不同的索引名称(在您的情况下:测试环境).
例如,您可以在中创建初始化程序
config/initializers/tire.rb
Run Code Online (Sandbox Code Playgroud)
使用以下行:
Tire::Model::Search.index_prefix "#{Rails.application.class.parent_name.downcase}_#{Rails.env.to_s.downcase}"
Run Code Online (Sandbox Code Playgroud)
假设您有名为Customer,Order和Product的模型,请将以下代码放在test-startup/before-block/each-run-block的某处.
# iterate over the model types
# there are also ways to fetch all model classes of the rails app automaticly, e.g.:
# http://stackoverflow.com/questions/516579/is-there-a-way-to-get-a-collection-of-all-the-models-in-your-rails-app
[Customer, Order, Product].each do |klass|
# make sure that the current model is using tire
if klass.respond_to? :tire
# delete the index for the current model
klass.tire.index.delete
# the mapping definition must get executed again. for that, we reload the model class.
load File.expand_path("../../app/models/#{klass.name.downcase}.rb", __FILE__)
end
end
Run Code Online (Sandbox Code Playgroud)
另一种方法是设置一个不同的ElasticSearch实例来测试另一个端口,比方说1234.在你的enviornment/test.rb中,你可以设置
Tire::Configuration.url "http://localhost:1234"
Run Code Online (Sandbox Code Playgroud)
在适当的位置(例如您的测试启动),您可以删除ElasticSearch测试实例上的所有索引:
Tire::Configuration.client.delete(Tire::Configuration.url)
Run Code Online (Sandbox Code Playgroud)
也许您仍然必须确保您的模型类的轮胎映射定义仍然被调用.
mha*_*rah 11
在我的rspec套件中通过轮胎删除弹性搜索索引时,我遇到了一个奇怪的错误.在我的Rspec配置中,类似于Bits和Bytes博客,我有一个after_each调用,它清理数据库并清除索引.
我发现我需要调用Tire的create_elasticsearch_index方法,该方法负责读取ActiveRecord类中的映射以设置适当的分析器等.我看到的问题是我在我的模型中有一些:not_analyzed字段实际上正在分析(这打破了我想要分面工作的方式).
开发方面的一切都很好,但测试套件失败了,因为各个单词而不是整个多字符串都破坏了方面.删除索引后,似乎没有在rspec中正确创建映射配置.添加create_elasticsearch_index调用修复了问题:
config.after(:each) do
DatabaseCleaner.clean
Media.tire.index.delete
Media.tire.create_elasticsearch_index
end
Run Code Online (Sandbox Code Playgroud)
媒体是我的模特课.
| 归档时间: |
|
| 查看次数: |
12403 次 |
| 最近记录: |