How do you define trait using Fabrication

五迷三道 提交于 2019-12-10 20:27:57

问题


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


来源:https://stackoverflow.com/questions/36065015/how-do-you-define-trait-using-fabrication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!