2013-03-05 8 views
7

私は、after_createが関連するオブジェクトを作成する特性を持つFG createを呼び出すテストをいくつか書いています。 FGを作ったときに関連する製品にパラメータを送信する方法はありますか、それとも後で設定する必要がありますか?どのようにFactoryGirl形質にパラメーターを送信するのですか?

+0

を行い、[このSOポスト](http://stackoverflow.com/questions/16297357/factorygirl-override-attribute-of-associated -object)あなたの質問に答えますか?あなたは 'after_create'ブロックをその特性に追加する点を除いて同じことをします。 – adarsh

答えて

7

無視して、そのパラメータを追加します。

FactoryGirl.define do 
    trait :my_trait do 
    ignore do 
     associated_attributes nil 
    end 

    after_create do |object, evaluator| 
     # Use the ignored associated_attributes when creating the associated object 
     associated_object = AssociatedModel.new(evaluator.associated_attributes) 
    end 
    end 
end 

そしてthis page has a lot more tricks

+1

ignoreは廃止され、FactoryGirl 5.0で削除されます。無視する代わりにtransientを使用できます [source](https://github.com/thoughtbot/factory_girl/issues/527) – Ekkstein

関連する問題