如何将params发送到FactoryGirl特征?

Mik*_*rin 7 testing rspec factory-bot

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

Sub*_*has 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
Run Code Online (Sandbox Code Playgroud)

这个页面有很多花样多

  • ignore已被弃用,将在FactoryGirl 5.0中删除,你可以使用transient而不是ignore [来源](https://github.com/thoughtbot/factory_girl/issues/527) (2认同)