How do I send params to a FactoryGirl trait?
Asked Answered
C

1

8

I'm writing some tests that call a FG create with a trait that after_create, makes an associated object. Is there a way to send parameters to that associated product when I make the FG, or do I need to set them afterwards?

Challah answered 5/3, 2013 at 19:0 Comment(1)
Does this SO post answer your question? You'd do the same thing except add the after_create block in the trait.Calcium
A
9

Add that parameter in 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

And this page has a lot more tricks

Arabeila answered 26/5, 2013 at 14:8 Comment(1)
ignore is deprecated and will be removed in FactoryGirl 5.0 you can use transient instead of ignore sourceLapsus

© 2022 - 2024 — McMap. All rights reserved.