Faker在factory_girl中使用时产生重复数据 [英] Faker is producing duplicate data when used in factory_girl

查看:89
本文介绍了Faker在factory_girl中使用时产生重复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Faker gem将一些虚假数据填充到工厂中:

I'm trying to populate some fake data into a factory using the Faker gem:

Factory.define :user do |user|
  user.first_name Faker::Name::first_name
  user.last_name Faker::Name::last_name
  user.sequence(:email) {|n| "user#{n}@blow.com" }
end

但是我希望这会产生具有不同的firstname和last_names的用户,但每个用户都是相同的:

However while I expect this to produce users who have different first_name and last_names, each one is the same:

>> Factory(:user)
=> #<User id: 16, email: "user7@blow.com", created_at: "2011-03-18 18:29:33",     
updated_at: "2011-03-18 18:29:33", first_name: "Bailey", last_name: "Durgan">
>> Factory(:user)
=> #<User id: 17, email: "user8@blow.com", created_at: "2011-03-18 18:29:39", 
updated_at: "2011-03-18 18:29:39", first_name: "Bailey", last_name: "Durgan">

如何获取Faker gem为每个用户生成新名称,而不仅仅是重用原始名称?

How can I get the Faker gem to generate new names for each users and not just reuse the original ones?

推荐答案

Factory.define :user do |user|
  user.first_name { Faker::Name::first_name }
  user.last_name { Faker::Name::last_name }
  user.sequence(:email) {|n| "user#{n}@blow.com" }
end

尝试在假冒者周围放置方括号.参见此链接

Try putting brackets around the fakers. see this link

这篇关于Faker在factory_girl中使用时产生重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆