如何在factory_girl中建立/创建多对多关联?
我有一个 Person
模型,它与 Email
模型具有多对多关系,我想创建一个工厂,让我为该模型生成名字和姓氏人(这已经完成)并根据该人的姓名创建一个电子邮件地址。这是我创建一个人
的名字:
Factory.sequence :first_name do |n|
first_name = %w[FirstName1 FirstName2] # ... etc (I'm using a real subset of first names)
first_name[(rand * first_name.length)]
end
Factory.sequence :last_name do |n|
last_name = %w[LastName1 LastName2] # ... etc (I'm using a real subset of last names)
last_name[(rand * last_name.length)]
end
Factory.define :person do |p|
#p.id ???
p.first_name { Factory.next(:first_name) }
p.last_name { Factory.next(:last_name) }
#ok here is where I'm stuck
#p.email_addresses {|p| Factory(:email_address_person_link) }
end
Factory.define :email_address_person_link do |eapl|
# how can I link this with :person and :email_address ?
# eapl.person_id ???
# eapl.email_address_id ???
end
Factory.define :email_address do |e|
#how can I pass p.first_name and p.last_name into here?
#e.id ???
e.email first_name + "." + last_name + "@test.com"
end
I have a Person
model that has a many-to-many relationship with an Email
model and I want to create a factory that lets me generate a first and last name for the person (this is already done) and create an email address that is based off of that person's name. Here is what I have for create a person
's name:
Factory.sequence :first_name do |n|
first_name = %w[FirstName1 FirstName2] # ... etc (I'm using a real subset of first names)
first_name[(rand * first_name.length)]
end
Factory.sequence :last_name do |n|
last_name = %w[LastName1 LastName2] # ... etc (I'm using a real subset of last names)
last_name[(rand * last_name.length)]
end
Factory.define :person do |p|
#p.id ???
p.first_name { Factory.next(:first_name) }
p.last_name { Factory.next(:last_name) }
#ok here is where I'm stuck
#p.email_addresses {|p| Factory(:email_address_person_link) }
end
Factory.define :email_address_person_link do |eapl|
# how can I link this with :person and :email_address ?
# eapl.person_id ???
# eapl.email_address_id ???
end
Factory.define :email_address do |e|
#how can I pass p.first_name and p.last_name into here?
#e.id ???
e.email first_name + "." + last_name + "@test.com"
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我想我现在明白你在问什么了。像这样的东西应该可以工作(未经测试,但我在另一个项目中做了类似的事情):
如上所述,使用第三个单独的工厂是可选的。就我而言,我并不总是想为每个测试生成关联,因此我创建了一个单独的工厂,仅在几个特定测试中使用。
Ok, I think I understand what you're asking now. Something like this should work (untested, but I've done something similar in another project):
As noted, using a third, separate factory is optional. In my case I didn't always want to generate the association for every test, so I made a separate factory that I only used in a few specific tests.
使用回调(有关更多信息,请参阅 FG 文档)。回调传递当前正在构建的模型。
我认为这有效。
您还可以通过研究使用 Faker gem 来节省一些工作,它可以创建真实的名字和姓氏以及 e - 您的邮件地址。
Use a callback (see FG docs for more info). Callbacks get passed the current model being built.
I think that works.
You could also save yourself some work by looking into using the Faker gem which creates realistic first and last names and e-mail addresses for you.