如何在Factory Girl中创建/构建工厂的多个实例?

时间:2011-04-01 06:14:56

标签: ruby-on-rails factory-bot

如何创建同一类的多个记录或多个工厂?

我试过了:

Factory.define :user do |user|
  user.email "someuser@somesite.com"
  user.password "somepassword"

  user.email "another_existing_user@somesite.com"
  user.password "somepassword"
end

Factory.define :user do |user|
  user.email "someuser@somesite.com"
  user.password "somepassword"
end

Factory.define :user do |user|
  user.email "another_existing_user@somesite.com"
  user.password "somepassword"
end

但它不起作用 - Attribute already defined: email

4 个答案:

答案 0 :(得分:43)

这是一个较旧的问题和答案,但这是我在Google上找到的第一个结果,所以我想我会在标题构建或创建多个记录标题下的docs添加以下内容:

created_users = FactoryGirl.create_list(:user, 25)  #creates 25 users

twenty_year_olds = FactoryGirl.build_list(:user, 25, date_of_birth: 20.years.ago)  #builds 25 users, sets their date_of_birth

如果您想在rails控制台中运行此功能,请在此处考虑以下答案:https://stackoverflow.com/a/23580836/4880924

在我刚引用的示例中,每个用户都有不同的用户名,只要在工厂定义中使用sequence(请参阅上面的Mike Lewis答案)。

答案 1 :(得分:32)

如果您希望来自同一(基地)工厂的记录具有不同的值,则有几个选项。

A)覆盖已定义的属性

factory :post, aliases: [:approved_post] do
  title "A title"
  approved true
end

approved_post = create(:approved_post)
unapproved_post = create(:post, approved: false)

B)继承

factory :post do
  title "A title"

  factory :approved_post do
    approved true
  end

  factory :unapproved_post do
    approved false
  end
end

approved_post = create(:approved_post)
unapproved_post = create(:unapproved_post)

C)序列

factory :user do
  sequence(:email, 1000) { |n| "person#{n}@example.com" }
end

D)特征

factory :post do
  title "My awesome title"

  trait(:approved) { approved true }

  trait(:unapproved) { approved false }

  trait :with_comments do
    after(:create) do |instance|
      create_list :comment, 2, post: instance
    end
  end

  factory :approved_post_with_comments, traits: [:approved, :with_comments]
end

approved_post_with_comments = create(:approved_post_with_comments)
unapproved_post_with_no_comments = create(:post, :unapproved, title: "Test")
post_with_title = build(:post)

可以组合这些方法。此示例使用列表和对与序列和覆盖。

factory :user do
  sequence(:username) { |n| "user#{n}" }
  date_of_birth Date.today
end

# Build a pair and a list of users.
two_newborns     = build_pair(:user)
ten_young_adults = build_list(:user, 10, date_of_birth: 20.years.ago)

# Create a pair and a list of users.
two_young_adults = create_pair(:user, date_of_birth: 20.years.ago)
ten_newborns     = create_list(:user, 10)

我更喜欢尽可能使用特征,我觉得它们很灵活。

答案 2 :(得分:22)

使用工厂有两个步骤,第一步是定义它们,第二步是使用它们。

1)定义他们:

Factory.define :user do |u|
  u.sequence(:email) { |n| "mike#{n}@awesome.com"}
  u.password "password123"
end

2)使用他们:

一个例子是在规范中使用它们:

 @user1 = Factory(:user) #has an email of mike1@awesome.com
 @user2 = Factory(:user) # has an email of mike2@awesome.com due to sequences in FG

我会看到这个Railscast以便更好地感受它。

答案 3 :(得分:0)

使用当前factory_girl_rails 4.6.0我遇到了某种相关的问题,build(:model)总是返回该对象的同一个实例。使用sequence(:name) ...并没有解决这个问题。所以我生成了一些假的(空的)特征:

FactoryGirl.define do
  factory :model do
     ...
     # fake traits to urge factory_girl to always return a new instance:
    (1..5).each {|n| trait "var#{n}".to_sym }
  end
end

然后拨打build(:model, :var1), build(:model, :var2)...