rspec ActiveRecord :: RecordNotFound:无法找到带有' id' =的提案

时间:2016-07-27 09:05:40

标签: ruby-on-rails rspec

我运行测试,显示错误。

故障:

1)ContractsController POST #create具有有效属性重定向到付款页面      失败/错误:@ 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

1 个答案:

答案 0 :(得分:0)

由于FactoryGirl#attributes_for的使用,我确定您收到此错误。为什么?使用attributes_for方法时,它会返回资源的非持久哈希属性。然而关于attributes_for的事情是它不尊重关联,这是有道理的(为了保持FactoryGirl ORM不可知)。建议的方法是使用或定义自定义策略:

build(:contract).attributes

Find more useful references here