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

发布于 2024-10-30 03:50:01 字数 1069 浏览 4 评论 0原文

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

我尝试过:

Factory.define :user do |user|
  user.email "[email protected]"
  user.password "somepassword"

  user.email "[email protected]"
  user.password "somepassword"
end

Factory.define :user do |user|
  user.email "[email protected]"
  user.password "somepassword"
end

Factory.define :user do |user|
  user.email "[email protected]"
  user.password "somepassword"
end

它不起作用 - 属性已定义:电子邮件

How do I create multiple records or multiple factories of the same class?

I tried:

Factory.define :user do |user|
  user.email "[email protected]"
  user.password "somepassword"

  user.email "[email protected]"
  user.password "somepassword"
end

and

Factory.define :user do |user|
  user.email "[email protected]"
  user.password "somepassword"
end

Factory.define :user do |user|
  user.email "[email protected]"
  user.password "somepassword"
end

But it doesn't work -- Attribute already defined: email.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜味超标? 2024-11-06 03:50:01

这是一个较旧的问题和答案,但这是我在 Google 上找到的第一个结果,因此我想我应该从 docs 标题下构建或创建多个记录

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

twenty_year_olds = FactoryBot.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 的回答)。

This is an older question and answer but it was the first result I found on Google so I thought I would add the following from the docs under the heading Building or Creating Multiple Records:

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

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

If you want to run this in the rails console, consider the following answer here: https://stackoverflow.com/a/23580836/4880924

In the example I just cited, each user would have a different username, provided that sequence is used in the factory definition (see Mike Lewis' answer above).

疑心病 2024-11-06 03:50:01

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

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)

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

There's a couple of options if you want records from the same (base) factory to have different values.

A) Override defined attributes

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

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

B) Inheritance

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) Sequences

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

D) Traits

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)

These methods can be combined. This example uses lists and pairs with sequences and overriding.

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)

I prefer to use traits whenever possible, I find them flexible.

薄情伤 2024-11-06 03:50:01

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

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 [email protected]
 @user2 = Factory(:user) # has an email of [email protected] due to sequences in FG

我会观看这个 Railscast 以获得更好的感觉。

There are two steps to using Factories, the first is to define them, and the second is to use them.

1) Define Them:

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

2) Using Them:

An example would be to use them in a spec:

 @user1 = Factory(:user) #has an email of [email protected]
 @user2 = Factory(:user) # has an email of [email protected] due to sequences in FG

I'd watch this Railscast to get a better feel for it.

别把无礼当个性 2024-11-06 03:50:01

在当前的 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)...

With the current factory_girl_rails 4.6.0 I had the somehow related problem that build(:model) returned always the same instance of the object. Using sequence(:name) ... didn't fix that. So I generated some fake (empty) traits like this:

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

And then calling build(:model, :var1), build(:model, :var2)...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文