2016-05-09 23 views
2

私は、関連するオブジェクト(AR/Postgres)に属性を委譲しており、ファクトリを呼び出すときにそれを上書きしようとしています。factory_girlの委任属性を上書きする方法は?

class Patient 
    delegate :email, to: :credential 
    has_one :credential, as: :owner, dependent: :destroy 
end 

class Credential 
    belongs_to :owner, polymorphic: true 
end 

FactoryGirl.define do 
    factory :patient do 
    first_name 'Jane' 
    last_name 'Patient' 
    dob Date.parse('2005-01-11') 
    gender 'Female' 
    zipcode '80202' 

    after(:build) do |user| 
     create(:credential, owner: user) 
    end 
    end 
end 

FactoryGirl.define do 
    factory :credential do 
    password '!Secret15' 
    password_confirmation { password } 
    agree_to_terms true 

    sequence :email do |n| 
    "user#{n}@bar.com" 
    end 
    end 
end 

そしてコール:

create(:patient, email: '[email protected]') 

それは単純に動作していない、と属性がデフォルトの順序で設定されてきています。

彼らのドキュメントとGoogleを磨き、答えを見つけることができませんでした。どんな助けでも大歓迎です。

答えて

0

factory_girlはdelegateで何もしません。あなた自身:credentialemailを渡す必要があります。ありがとう、https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#transient-attributes

+0

恐ろしい:属性は、コールバックでは利用できないので、コールバックブロックの2番目の引数である評価者からemailを得る:

FactoryGirl.define do factory :patient do # other attributes after(:build) do |user, evaluator| create(:credential, owner: user, email: evaluator.email) end end end 

ここに文書化 – idknox

関連する問題