Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement :with_trait using Fabrication

I'm considering migrating many mocks from FactoryGirl over to the Fabrication gem.

However, so far I've been unable to find any information on implementing the trait pattern available in FactoryGirl.

Is there a generally accepted way to do this with Fabrication?

Thank you in advance for any answers or information.

like image 390
ocodo Avatar asked Apr 05 '15 08:04

ocodo


1 Answers

Fabrication doesn't have syntax sugar for traits. As far as I understand, it is just a way to group and define inheritance.

In the case of this Factory: (which I pulled from this blog post)

FactoryGirl.define do
  factory :todo_item, aliases: [:incomplete_todo_item] do
    name 'Pick up a gallon of milk'
    complete false

    factory :complete_todo_item do
      complete true
    end
  end
end

You would do the same thing in Fabrication like this:

Fabricator(:todo_item, aliases: :incomplete_todo_item) do
  name 'Pick up a gallon of milk'
  complete false
end

Fabricator(:complete_todo_item, from: :todo_item) do
  complete true
end

If you do decide to convert you can send to the mailing list with any specific questions. I am always happy to help figure out how to get things working or improve the efficiency of your fabricators.

like image 165
Paul Elliott Avatar answered Oct 30 '22 05:10

Paul Elliott