1

我正在使用 factory_girl_rails 和 rspec 并遇到麻烦,引发以下错误

   1) WebsiteLink when link is external
         Failure/Error: website_link.external should be false

           expected #<FalseClass:0> => false
                got #<WebsiteLink:100584240> => #<WebsiteLink id: nil, website_id: nil, link: nil, external: nil, checked: nil, click_count: nil, transition_count: nil, created_at: nil, updated_at: nil, link_description: nil>

           Compared using equal?, which compares object identity,
           but expected and actual are not the same object. Use
           `expect(actual).to eq(expected)` if you don't care about
           object identity in this example.

这是我在 spec.rb 文件中的代码

it "when link is external" do
    website = FactoryGirl.create(:website,site_address: "www.socpost.ru")
    website_link = FactoryGirl.create(:website_link, link: "www.google.com", website: website)
    website_link.external should be true
  end

工厂_女孩工厂

FactoryGirl.define do
 factory :website do
    sequence(:site_name){ |i| "Facebook#{i}" }
    sequence(:site_address){ |i| "www.facebook_#{i}.com" }
    sequence(:website_key){ |i| (1234567 + i).to_s }
  end
  factory :website_link do
    sequence(:link){ |i| "www.facebook.com/test_#{i}" }
    external false
    checked false
    click_count 1
    transition_count 1
    link_description "Hello"
    website
  end
end
4

2 回答 2

5

由于我认为了解您收到错误的原因是有帮助的,因此这里有一个解释:

  • 你的语句有四个用空格分隔的表达式:website_link.external, should,befalse
  • Ruby 从右到左评估这些
  • false微不足道
  • be被解释为false作为参数的方法调用
  • should被解释为一种方法,其结果为be作为参数。
  • should相对于 进行解释subject,因为该方法未发送到特定对象
  • 鉴于您收到的错误,subject要么显式设置为,要么WebsiteLink是示例父级的参数describe,因此隐式subject
  • website_link.external从未得到评估,因为错误发生在该点之前
于 2013-09-27T16:29:16.223 回答
1

你忘了使用dot.

it "when link is external" do
  website = FactoryGirl.create(:website,site_address: "www.socpost.ru")
  website_link = FactoryGirl.create(:website_link, link: "www.google.com", website: website)
  website_link.external.should be true (note the dot between external and should)
end

试试看。

于 2013-09-27T16:16:14.167 回答