2012-03-12 123 views
0

這裏是我的工廠:Rails/Rspec/Factory girl:記錄不關聯?

FactoryGirl.define do 
    factory :account do 
    company 'Example, Inc.' 
    end 

    factory :site do 
    association :account 
    end 

    factory :page do 
    association :site 
    end 
end 

和一個簡單的請求,規格:

require 'spec_helper' 

describe "Pages" do 

    before do 
    @account = Factory(:account) 
    @site = Factory(:site) 
    end 

    it "lets me create a new page" do 
    visit account_site_pages_path(@account, @site) 
    page.should have_content('New Page') 
    end 

end 

但我得到這個故障:

Failure/Error: visit account_site_pages_path(@account, @site) 
ActiveRecord::RecordNotFound: 
Couldn't find Site with ID=51 [WHERE (`sites`.account_id = 127)] 
# <internal:prelude>:10:in `synchronize' 
# ./spec/requests/pages_spec.rb:19:in `block (2 levels) in <top (required)>' 

這意味着,網站的工廠不與適當的關聯帳戶,對吧?我猜我錯過了很明顯的東西:D

回答

0

您的@account@site未連接,這可能是問題所在。因此,請執行以下操作:

before do 
    @account = Factory(:account) 
    @site = Factory(:site, :account => @account) 
end 

這樣,您的網站就不會創建新帳戶,而是與原始帳戶相關。

+0

是的,就是這樣。謝謝! – imderek 2012-03-12 12:22:11