2016-07-27 89 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

相关问题