如何在Factory Girl中创建/构建工厂的多个实例? [英] How to create/build multiple instances of a factory in Factory Girl?
本文介绍了如何在Factory Girl中创建/构建工厂的多个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何创建多个记录或同一类的多个工厂?
How do I create multiple records or multiple factories of the same class?
我尝试过:
Factory.define :user do |user|
user.email "someuser@somesite.com"
user.password "somepassword"
user.email "another_existing_user@somesite.com"
user.password "somepassword"
end
和
Factory.define :user do |user|
user.email "someuser@somesite.com"
user.password "somepassword"
end
Factory.define :user do |user|
user.email "another_existing_user@somesite.com"
user.password "somepassword"
end
但是它不起作用-Attribute already defined: email
.
推荐答案
使用工厂有两个步骤,第一步是定义工厂,第二步是使用工厂.
There are two steps to using Factories, the first is to define them, and the second is to use them.
1)定义它们:
Factory.define :user do |u|
u.sequence(:email) { |n| "mike#{n}@awesome.com"}
u.password "password123"
end
2)使用它们:
一个示例是在规范中使用它们:
An example would be to use them in a spec:
@user1 = Factory(:user) #has an email of mike1@awesome.com
@user2 = Factory(:user) # has an email of mike2@awesome.com due to sequences in FG
我会看这个 Railscast 以获得更好的感觉.
I'd watch this Railscast to get a better feel for it.
这篇关于如何在Factory Girl中创建/构建工厂的多个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文