如何将params发送到FactoryGirl特征?

时间:2013-03-05 19:00:53

标签: testing rspec factory-bot

我正在编写一些调用FG创建的测试,其特征是after_create,创建一个关联对象。有没有办法在制作FG时将参数发送到相关产品,或者之后是否需要设置它们?

1 个答案:

答案 0 :(得分:9)

在ignore中添加该参数:

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