2016-07-26 2 views
1

공장 클라이언트 및 계약을 생성했습니다. 나는 시험 만 표시 오류FactoryGirl :: AttributeDefinitionError : 속성이 이미 정의되었습니다. 사용자

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 

지원/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 

공장 또는 테스트의 문제점은 무엇입니까?

+1

'에 대한 공장 출하시 기능 : contract'이 심지어 공장의 이름을두면,이를 단축 할 수 있습니다? –

+0

질문이 업데이트되었습니다. – Dmitrij

+0

'association : user'의 정의를 두 번 이해하지 못하겠습니까? – kasperite

답변

2

공장 :contract은 허용되지 않는 user이라는 두 개의 속성을 정의합니다.

는 예컨대, 그들 고유 라벨 (공장에서) 부여 :
FactoryGirl.define do 
    factory :contract do 
    sequence(:title) { |n| "translation#{n}" } 
    amount 150 
    additional_information 'X' * 500 
    due_date { 21.days.from_now } 

    association :client, factory: :client 
    association :contractor, factory: :contractor 
    end 
end 

가 맞는 것으로

, 나는 공장 이름으로 해당 속성 이름을 선택했습니다. ("협회" http://www.rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md 참조 섹션 :

If the factory name is the same as the association name, the factory name can be left out.

)

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

    client 
    contractor 
    end 
end 

:

관련 문제