如何测试轮胎indexed_json的结果?

时间:2012-12-26 15:14:50

标签: ruby-on-rails ruby ruby-on-rails-3 elasticsearch tire

我想测试我的搜索方法,但是如何测试to_indexed_json结果。

这是我的测试:

describe Search do
  before do
    Question.index.delete
    Question.tire.create_elasticsearch_index
    app = create :app, name: "Marketing", id: 76
    @question1 = create :question, content: "Some super question?", app: app
    @question2 = create :question, content: "Some extra question?", app: app
  end

  describe "#results" do
    it "returns matched results" do
      Search.new("Some", \[76\]).results.should == \[@question1, @question2\]
    end
  end
end

这是我收到的错误:

1 个答案:

答案 0 :(得分:2)

首先,您并未在发布的代码中测试to_indexed_json - 您正在测试对搜索结果的一些期望。这很好,实际上更合理的事情:),但仍然是另一回事。

其次,在集成测试中,请确保在索引测试数据后调用Tire::Index#refresh,以便它们立即可用于搜索 - 默认延迟为1秒。

第三,您在代码中创建了一些测试模型,然后期望返回这些确切的对象。要使 Tire 像这样,使用:load => true选项(从数据库加载模型),不设置对象等效的期望,但测试例如。模型的某些属性(例如content)。

相关问题