问题描述
我正在关注Michael Hartl的在线教程,在清单9.42中,我很难理解代码.
I am following Michael Hartl's online tutorial and in Listing 9.42, I am having trouble comprehending the code.
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
password "foobar"
password_confirmation "foobar"
factory :admin do
admin true
end
end
end
然后在清单9.43中创建管理员
Then admin is created in listing 9.43
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
我不明白的是,如何在没有任何管理员的情况下创建管理员
What I don't understand is how that is possible to create an admin without anycode of
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
password "foobar"
password_confirmation "foobar"
在管理员区里面?
似乎:admin块嵌套在:user块内,所以:user块代码是在FactoryGirl.create(:admin)
创建用户名为admin的admin的过程中执行的::user块内?
It seems :admin block is nested inside :user block and so the :user block code is executed during FactoryGirl.create(:admin)
creating an admin with users name, email, password inside the :user block?
是吗?谢谢!
推荐答案
假定您已经知道 FactoryGirl库的工作原理,对您问题的解释是:admin
工厂是在:user
工厂内部定义的
Assuming you already know how FactoryGirl library works, the explanation to your question is that the :admin
factory is defined inside the :user
factory
FactoryGirl.define do
factory :user do
...
factory :admin do
admin true
end
end
end
在这种情况下,:admin
将继承用户的所有属性以及特定的admin: true
设置.
In this case, :admin
will inherit all the properties of the user, plus the specific admin: true
setting.
FactoryGirl.create(:admin)
这篇关于Factorygirl管理员创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!