Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send params to a FactoryGirl trait?

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?

like image 462
Mike Manfrin Avatar asked Mar 05 '13 19:03

Mike Manfrin


1 Answers

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

like image 52
Subhas Avatar answered Oct 05 '22 08:10

Subhas