2013-09-27 41 views
1

我使用factory_girl_rails和RSpec和陷入困境, 引发folloing错误工厂女孩和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 

的factory_girl代码工厂

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 

回答

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 

试试看。

5

因为我认为这是有助于了解为什么您收到您收到的错误,这里是一个解释:

  • 你发言了四个表达式用空格分隔:website_link.externalshouldbefalse
  • 红宝石评估这些由右至左
  • false是微不足道
  • be被解释为以false作为参数的方法调用
  • should被解释为以be的结果作为参数的方法。
  • should相对于subject解释,由于该方法未发送至给定收到,subject被显式设定为WebsiteLink,或者是参数describe为的父该错误的特定对象
  • 例如,从而隐含subject
  • website_link.external从来没有得到评估,因为在此之前,点
发生错误