【问题标题】:rspec ActiveRecord::RecordNotFound: Couldn't find Proposal with 'id'=rspec ActiveRecord::RecordNotFound: 找不到带有 'id'= 的提案
【发布时间】:2016-07-27 17:23:30
【问题描述】:

我运行测试,显示错误。

失败:

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

【问题讨论】:

  • 除了资源的创建,在你的创建动作中还会发生什么?

标签: ruby-on-rails rspec


【解决方案1】:

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

build(:contract).attributes

Find more useful references here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多