2012-12-26 63 views
0

我想测试我的搜索方法,但是如何测试to_indexed_json的结果。如何测试轮胎的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 

这是错误,我得到:

回答

2

首先,你没有真正在发布代码的测试to_indexed_json - 你”重新测试一些关于搜索结果的期望。这很好,而且实际上更合理的做法:),但仍然是一个不同的事情。

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

第三,您在代码中创建了一些测试模型,然后期望返回这些确切的对象。为了使轮胎的行为如此,请使用:load => true选项(从数据库加载模型),不要设置关于对象等价的期望值,但可以测试例如。您的模型的一些属性(例如content)。

相关问题