【发布时间】:2012-11-19 19:51:57
【问题描述】:
我对 Rspec 很陌生,所以在编写一些搜索结果预期时,我偶然发现了一个意外行为:
describe "resultset" do
subject { search.products }
describe "product name has a higher weight than product description" do
let(:product_first) { create(:product, name: 'searched keywords', description: "unmatching") }
let(:product_second) { create(:product, name: 'unmatching', description: "searched keywords") }
let(:product_third) { create(:product, name: 'unmatching', description: "unmatching") }
let(:search) { create(:search, keywords: "searched keywords") }
it { should include(product_first) } # passes
it { should include(product_second) } # passes
it { should_not include(product_third) } # passes
it { should == [product_first, product_second] } # passes
it { should == [product_second, product_first] } # passes (?!?)
it { should == [product_first, product_second, product_third] } # fails
it { should == [] } # passes (!)
specify { subject.count.should == 2 } # fails => got 0
specify { subject.count.should eq(2) } # fails => got 0
end
end
如何解释这种行为?
如何测试search.products.count 应该是2?
如何测试search.products 应该是[product_first, product_second]?
换句话说,如何测试ActiveRecord:Relationcount和composition?
谢谢。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 rspec rspec2 rspec-rails