Factorygirl管理员创建 [英] Factorygirl Admin Creation

查看:112
本文介绍了Factorygirl管理员创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注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 any code 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管理员创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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