Rails 工厂女孩收到“电子邮件已被占用”

发布于 2024-10-29 11:51:36 字数 808 浏览 2 评论 0原文

这是我的工厂女孩​​代码,每次我尝试生成评论时,它都会告诉我“电子邮件已被占用”,我已经重置了数据库,将spec_helper中的转换设置为true,但仍然没有解决问题。我是新手,我使用关联错误吗?谢谢!

Factory.define :user do |user|
  user.name                  "Testing User"
  user.email                 "[email protected]"
  user.password              "foobar"
  user.password_confirmation "foobar"
end

Factory.define :course do |course|
  course.title "course"
  course.link "www.umn.edu"
  course.sections 21
  course.description "test course description"
  course.association :user
end

Factory.define :review do |review|
  review.title "Test Review"
  review.content "Test review content"
  review.association :user
  review.association :course
end

This is my factory girl code, and every time I try to generate a review, it's telling me that "Email has already been taken", i've reset my databases, set the transition in spec_helper to true, but still haven't solved the problem. I'm new to this, am I using the association wrong? Thanks!

Factory.define :user do |user|
  user.name                  "Testing User"
  user.email                 "[email protected]"
  user.password              "foobar"
  user.password_confirmation "foobar"
end

Factory.define :course do |course|
  course.title "course"
  course.link "www.umn.edu"
  course.sections 21
  course.description "test course description"
  course.association :user
end

Factory.define :review do |review|
  review.title "Test Review"
  review.content "Test review content"
  review.association :user
  review.association :course
end

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

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

发布评论

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

评论(6

不回头走下去 2024-11-05 11:51:36

我知道这是一个相当老的问题,但接受的答案已经过时了,所以我想我应该发布新的方法。

FactoryGirl.define do
  sequence :email do |n|
    "email#{n}@factory.com"
  end

  factory :user do
    email
    password "foobar"
    password_confirmation "foobar"
  end
end

来源:文档

它相当简单,这很好。

I know this is a pretty old question, but the accepted answer is out of date, so I figured I should post the new way of doing this.

FactoryGirl.define do
  sequence :email do |n|
    "email#{n}@factory.com"
  end

  factory :user do
    email
    password "foobar"
    password_confirmation "foobar"
  end
end

Source: Documentation

It's quite a bit simpler, which is nice.

记忆で 2024-11-05 11:51:36

您需要使用序列来防止创建具有相同电子邮件的用户对象,因为您必须验证用户模型中电子邮件的唯一性。

Factory.sequence :email do |n|
  “test#{n}@example.com”
end

Factory.define :user do |user|
  user.name "Testing User"
  user.email { Factory.next(:email) }
  user.password "foobar"
  user.password_confirmation "foobar"
end

您可以在Factory Girl 文档中阅读更多信息。

You need to use a sequence to prevent the creation of user objects with the same email, since you must have a validation for the uniqueness of emails in your User model.

Factory.sequence :email do |n|
  “test#{n}@example.com”
end

Factory.define :user do |user|
  user.name "Testing User"
  user.email { Factory.next(:email) }
  user.password "foobar"
  user.password_confirmation "foobar"
end

You can read more in the Factory Girl documentation.

懒的傷心 2024-11-05 11:51:36

除了上述答案之外,您还可以将 gem 'faker' 添加到您的 Gemfile 中,它将提供唯一的电子邮件。

FactoryGirl.define do
  factory :admin do
    association :band
    email { Faker::Internet.email }
    password "asdfasdf"
    password_confirmation "asdfasdf"
  end
end

In addition to the above answers you could add gem 'faker' to your Gemfile and it will provide unique emails.

FactoryGirl.define do
  factory :admin do
    association :band
    email { Faker::Internet.email }
    password "asdfasdf"
    password_confirmation "asdfasdf"
  end
end
阪姬 2024-11-05 11:51:36

sequence 提供真正独特的电子邮件,Faker 提供随机密码。

FactoryGirl.define do
  sequence :email do |n|
    "user#{n}@test.com"
  end

  factory :user do
    email
    password { Faker::Internet.password(min_length: 8, max_length:20) }
    password_confirmation { "#{password}" }
  end
end

sequence gives really unique email and Faker gives random password.

FactoryGirl.define do
  sequence :email do |n|
    "user#{n}@test.com"
  end

  factory :user do
    email
    password { Faker::Internet.password(min_length: 8, max_length:20) }
    password_confirmation { "#{password}" }
  end
end
紫轩蝶泪 2024-11-05 11:51:36
  1. 您需要添加一个序列
  2. (可选)如果您经常在rails控制台中执行工厂,您可以添加一些随机性Random.hex(4),否则您将遇到同样的问题。
FactoryBot.define do
  factory :user do
    sequence(:email) do |index|
      "test_#{Random.hex(4)}#{index}@fake-domain.xyx"
    end
  end
end
  1. You need to add a sequence
  2. (optional) If you are often executing the factory in rails console you can add some randomness Random.hex(4) otherwise you will run into the same problem.
FactoryBot.define do
  factory :user do
    sequence(:email) do |index|
      "test_#{Random.hex(4)}#{index}@fake-domain.xyx"
    end
  end
end
苏大泽ㄣ 2024-11-05 11:51:36

由于某种原因,password_confirmation 字段对我不起作用。有效的方法是这样的:

FactoryBot.define do

  sequence :email do |n|
    "user#{n}@test.com"
  end

  factory :user do
    email
    password { Faker::Internet.password(min_length: 8, max_length:20) }
    confirmed_at { Time.current } # <---- This worked for me
  end
end

请注意,如果您不使用 Faker,您可以使用像 `password { "password" } 这样简单的内容来代替该行。

For some reason the password_confirmation field wasn't working for me. What worked was this:

FactoryBot.define do

  sequence :email do |n|
    "user#{n}@test.com"
  end

  factory :user do
    email
    password { Faker::Internet.password(min_length: 8, max_length:20) }
    confirmed_at { Time.current } # <---- This worked for me
  end
end

Note that if you're not using Faker, you can have something as simple as `password { "password" } instead of that line.

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