Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you define trait using Fabrication

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

like image 409
python Avatar asked Dec 17 '25 11:12

python


1 Answers

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
like image 98
Axxiss Avatar answered Dec 20 '25 07:12

Axxiss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!