轮胎宝石:文件是否存在于索引中?

时间:2013-11-07 11:23:25

标签: ruby rspec elasticsearch

如何使用tire gem检入rspec测试:ElasticSearch索引中是否存在文档?

示例:

#model
class Car
  include Mongoid::Document
  include include Tire::Model::Search
  include Tire::Model::Callbacks
  #...
end

#spec
record = Car.first
record.tire.exist_in_index?.should be_true #wrong code

或者可能是其他任何解决方法。

1 个答案:

答案 0 :(得分:1)

您应该创建自定义匹配器:

RSpec::Matchers.define :be_in_search_index do |expected|
  match do |actual|
    !!actual.tire.index.retrieve(actual.class.name.underscore, actual.id.to_s)
  end
end

#...
record.should be_in_search_index