您能否通过代码向我展示如何使用Fabrication
转换此特定factory_girl
代码?
factory :user do
name "Friendly User"
login { name }
trait :male do
name "John Doe"
gender "Male"
login { "#{name} (M)" }
end
trait :female do
name "Jane Doe"
gender "Female"
login { "#{name} (F)" }
end
trait :admin do
admin true
login { "admin-#{name}" }
end
factory :male_admin, traits: [:male, :admin] # login will be "admin-John Doe"
factory :female_admin, traits: [:admin, :female] # login will be "Jane Doe (F)"
end
如果你在制造中看到这个特性inheritance
是这里的方法之一,但问题是我们无法为它定义Fabricator
,因为它们不是models
。谁能告诉我我们如何在Fabrication
中对属性进行分组?
你在捏造中这样表达它:
Fabricator :user do
admin false
gender 'Female'
name "Friendly User"
login do |attrs|
if attrs[:admin]
"admin-#{attrs[:name]}"
else
"#{attrs[:name]} (#{attrs[:gender][0]})"
end
end
end
Fabricator(:female_admin, from: :user) do
admin true
end
Fabricator(:male_admin, from: :female_admin) do
gender 'Male'
end