2013-09-27 69 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從來沒有得到評估,因爲在此之前,點
發生錯誤