2016-07-27 146 views
0

我運行測試,顯示錯誤。rspec ActiveRecord :: RecordNotFound:無法找到提案'ID'=

失敗:

1)ContractsController POST#創建與有效屬性重定向到支付頁面 故障/錯誤:@proposal = Proposal.find(PARAMS [:PROPOSAL_ID])

ActiveRecord::RecordNotFound: 
    Couldn't find Proposal with 'id'= 

需要' rails_helper」

describe ContractsController do 
    login_client 

    describe 'POST #create' do 
    let(:proposal) { create(:proposal) } 
    let(:contract) { create(:contract) } 

    context 'with valid attributes' do 
     it 'redirects to payment page' do 
     post :create, contract: attributes_for(:contract) 
     expect(response).to redirect_to payment_new_path 
     end 
    end 
    end 
end 

工廠女孩:

FactoryGirl.define do 
    factory :contract do 
    sequence(:title) { |n| "translation#{n}" } 
    amount 150 
    additional_information 'X' * 500 
    due_date { 21.days.from_now } 

    proposal 
    client 
    contractor 
    end 
end 

FactoryGirl.define do 
    factory :proposal do 
    description text 
    amount 150 

    project 
    user 
    end 
end 
+0

除了創建資源,還有什麼在你創造的行動會發生什麼? – oreoluwa

回答

0

我相信你會因爲使用FactoryGirl#attributes_for而得到這個錯誤。爲什麼?當您使用attributes_for方法時,它會爲該資源返回一個非持久散列屬性。然而,關於attributes_for的事情是它不尊重關聯,這是有道理的(爲了保持FactoryGirl ORM不可知)。對此的一種建議的方法是使用或定義自定義策略:

build(:contract).attributes 

Find more useful references here

相關問題