2013-03-05 26 views
7

我正在寫一些測試,調用FG創建一個特性,after_create,使關聯的對象。在製作FG時是否有辦法向相關產品發送參數,或者是否需要在之後設置參數?如何發送參數給FactoryGirl特性?

+0

不[本SO後(http://stackoverflow.com/questions/16297357/factorygirl-override-attribute-of-associated -object)回答你的問題?除了在特徵中添加'after_create'模塊之外,你會做同樣的事情。 – adarsh 2013-05-16 04:07:01

回答

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

忽略已棄用,並將在FactoryGirl 5.0中刪除,您可以使用暫時而不是忽略 [source](https://github.com/thoughtbot/factory_girl/issues/527) – Ekkstein 2016-11-04 12:51:44