如何使用制造来定义特征 [英] How do you define trait using Fabrication

查看:109
本文介绍了如何使用制造来定义特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在Rails中使用制造,我们已决定替换我们所有的factory_girl代码都带有伪造.

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

假设我们在factory_girl

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

您将如何在Fabrication中对此进行定义?我浏览了他们的网站,但找不到任何有关此的信息.感谢您的帮助

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

推荐答案

似乎您正在寻找

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

您可以使用:from属性从其他制造商继承属性.

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

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

设置:from选项将继承命名的制造者的类和所有属性.

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

您还可以显式指定使用:class_name参数构造的类.

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

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

这篇关于如何使用制造来定义特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆