How do you define trait using Fabrication

2019-07-04 23:35发布

问题:

I am learning how to use fabrication in Rails and we have decided to replace all our factory_girl code with fabrication.

Suppose we have this code in factory_girl

FactoryGirl.define do
  factory :user do
  trait(:no_credits) { credits 0 }
  trait(:with_credits) { credits 300 }

How will you define this in Fabrication? I have gone through their website but couldn't find anything regarding this. Will appreciate your help

回答1:

Seems that your are looking for Fabricator inheritance, this is what the docs says:

You can inherit attributes from other fabricators by using the :from attribute.

Fabricator(:llc, from: :company) do
  type "LLC"
end

Setting the :from option will inherit the class and all the attributes from the named Fabricator.

You can also explicitly specify the class being fabricated with the :class_name parameter.

Fabricator(:llc, class_name: :company) do
  type "LLC"
end