【发布时间】:2016-07-27 05:22:21
【问题描述】:
我创建了工厂客户和合同。我运行测试,但显示错误
FactoryGirl.define do
factory :client, class: User do
role 'client'
first_name 'John'
sequence(:last_name) { |n| "client#{n}" }
sequence(:email) { |n| "client#{n}@example.com" }
# avatar { Rack::Test::UploadedFile.new(File.join(Rails.root, 'public', 'images', '128.jpg')) }
password 'password'
password_confirmation 'password'
end
end
support/controller_macros.rb
module ControllerMacros
def login_client
before do
@client = create(:client)
#@request.env['devise.mapping'] = Devise.mappings[:client]
sign_in @client
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 }
association :user, factory: :client
association :user, factory: :contractor
end
end
我运行测试 rspec 规范/控制器/contracts_controller_spec.rb
require 'rails_helper'
describe ContractsController do
login_client
let(:contract) { create(:contract) }
describe 'POST #create' do
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
错误显示:
Failure/Error: post :create, contract: attributes_for(:contract)
FactoryGirl::AttributeDefinitionError:
Attribute already defined: user
工厂或测试中有什么问题?
【问题讨论】:
-
:contract的工厂是什么? -
我更新了问题。
-
两次不太明白
association :user的定义,能详细点吗?
标签: ruby-on-rails ruby rspec