问题描述
我使用FactoryGirl和Faker在我的 seeds.rb
文件中生成用户对象,但由于某种原因,正在创建完全相同的用户,
用户的工厂:
$ b b
#users.rb
要求'faker'
FactoryGirl.define do
factory:user do
first_name Faker :: Name.first_name
last_name Faker :: Name.last_name
phone Faker :: PhoneNumber.cell_phone
email Faker :: Internet.email
passwordpassword
password_confirmationpassword
end
end
c $ c> seeds.rb 档案:
#seeds.rb
rand 5..11).times {FactoryGirl.create(:user)}
错误:
ActiveRecord :: RecordInvalid:验证失败:已发送电子邮件
如果我打开控制台并使用 FactoryGirl.create(:user)
我得到相同的结果...相同的对象被创建一遍又一遍,即使如果我运行 Faker :: Internet.email
几次我会收到几封电子邮件。
FactoryGirl: / p>
[1] pry(main)> FactoryGirl.create(:user)
...
=> #< User id:3,first_name:Osvaldo,last_name:Wunsch,email:[email protected],phone:(912)530-4949 x64848,created_at:2014-07-31 20:57:27,updated_at:2014-07-31 20:57:27,encrypted_password:$ 2a $ 10 $ mxWC7yLYR0m / Sw8MO6Lyru.xuTHCdCEuM9Orx3LXGApF ...,reset_password_token:nil,reset_password_sent_at:nil,remember_created_at:nil ,sign_in_count:0,current_sign_in_at:nil,last_sign_in_at:nil,current_sign_in_ip:nil,last_sign_in_ip:nil>
[2] pry(main)> FactoryGirl.create(:user)
...
ActiveRecord :: RecordInvalid:验证失败:电子邮件已被采取
Faker本身:
[3] Faker :: Internet.email
=> [email protected]
[4] pry(main)> Faker :: Internet.email
=> [email protected]
我在这里缺少什么?为什么Faker每次通过FactoryGirl使用时都会生成相同的数据?
Faker :: Internet.email
试试...
电子邮件{Faker :: Internet.email}
I am using FactoryGirl and Faker to generate user objects in my seeds.rb
file but for some reason the exact same user is being created and rake db:seed
is failing because of an email uniqueness validation.
Factory for users:
#users.rb
require 'faker'
FactoryGirl.define do
factory :user do
first_name Faker::Name.first_name
last_name Faker::Name.last_name
phone Faker::PhoneNumber.cell_phone
email Faker::Internet.email
password "password"
password_confirmation "password"
end
end
And the code in seeds.rb
file:
#seeds.rb
rand(5..11).times { FactoryGirl.create(:user) }
Error:
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
If I open the console and use FactoryGirl.create(:user)
I get the same results...same object is being created over and over even though if I run just Faker::Internet.email
several times I'll get several e-mails.
FactoryGirl:
[1] pry(main)> FactoryGirl.create(:user)
...
=> #<User id: 3, first_name: "Osvaldo", last_name: "Wunsch", email: "[email protected]", phone: "(912)530-4949 x64848", created_at: "2014-07-31 20:57:27", updated_at: "2014-07-31 20:57:27", encrypted_password: "$2a$10$mxWC7yLYR0m/Sw8MO6Lyru.xuTHCdCEuM9Orx3LXGApF...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil>
[2] pry(main)> FactoryGirl.create(:user)
...
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
Faker by itself:
[3] pry(main)> Faker::Internet.email
=> "[email protected]"
[4] pry(main)> Faker::Internet.email
=> "[email protected]"
What am I missing here? Why is Faker producing the same data every time when used through FactoryGirl?
You need to pass a block if you want the values re-evaluated for each instance created.
Instead of
email Faker::Internet.email
try...
email { Faker::Internet.email }
这篇关于FactoryGirl + Faker - 为db种子数据中的每个对象生成相同的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!