我对 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:Relation
计数和组成?
谢谢你。