gpt4 book ai didi

ruby-on-rails - FactoryGirl 电子邮件已被占用

转载 作者:太空宇宙 更新时间:2023-11-03 17:29:51 25 4
gpt4 key购买 nike

我正在尝试用 FactoryGirl 实例化多个对象(拥有唯一电子邮件的客人)

  guests = create_list(:guest, 3)

但是我得到错误:

 Failure/Error: guests = create_list(:guest, 3)

ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken

这里是我的工厂:

FactoryGirl.define do
factory :guest do
email Faker::Internet.unique.email
firstname 'John'
lastname 'Doe'
password 'test'
password_confirmation 'test'

factory :guest_with_bookings do
email "#{SecureRandom.hex}@test.com"
transient do
bookings_count 2
end
after(:create) do |guest, evaluator|
create_list(:booking, evaluator.bookings_count, guests: [guest])
end
end
end
end

即使使用 Faker::Internet.unique.email,当我尝试创建超过 1 个访客时它也不起作用

最佳答案

在你的工厂中使用顺序

FactoryGirl.define do
factory :guest do
sequence(:email) { |n| "name_#{n}@example.com" }

或者结合Faker

FactoryGirl.define do
factory :guest do
sequence(:email) { |n| Faker::Internet.unique.email("Nancy_#{n}") }

关于ruby-on-rails - FactoryGirl 电子邮件已被占用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44548897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com