0

我对 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 计数组成

谢谢你。

4

2 回答 2

1

怎么样:

it { should have(2).items }

因为您的主题是 search.products 并且由于其let工作方式(它仅在调用时创建变量),您可能希望强制创建您的第一个、第二个和第三个产品。只需将let(...)行更改为let!(...).

所以工作(和连贯)的规格是:

  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 == [product_first, product_second] } # passes
    it { should_not == [product_second, product_first] } # passes 
    it { should include(product_first) }     # passes
    it { should include(product_first) }     # passes
    it { should include(product_second) }    # passes
    it { should_not include(product_third) } # passes
    it { should have(2).items }   # passes
  end
于 2012-11-19T21:13:49.553 回答
1

我认为问题在于如何let工作。当您使用 定义变量时let,它实际上不会被评估,直到您在其中一个测试中引用它。product_first因此,直到您运行之后,您才真正创建对象search.products

我认为您可以通过使用来获得您想要的东西before,并将所有测试对象实例化为实例变量。就像是:

describe "resultset" do

  describe "product name has a higher weight than product description" do

    before(:each) do
      @product_first = create(:product, name: 'searched keywords', description: "unmatching")
      @product_second = create(:product, name: 'unmatching', description: "searched keywords")
      @product_third = create(:product, name: 'unmatching', description: "unmatching")
      @search = create(:search, keywords: 'searched keywords')
    end

    subject {@search.products}
    it { should include(@product_first) }     # passes
    ...
于 2012-11-19T21:15:17.687 回答