工厂女孩和协会
我有两个类和两个工厂:
class User
belongs_to :company
end
class Company
has_many :users
end
Factory.define :user do |u|
u.name "Max"
u.association :company
end
Factory.define :user2, :parent => :user do |u|
u.name "Peter"
end
Factory.define :company do |c|
c.name "Acme Corporation"
end
如何实现在同一家公司拥有两个用户?运行测试时,FactoryGirl 创建了两条公司记录,但我希望两个用户都连接到一条记录。
有什么提示吗?
I have two classes and two factories:
class User
belongs_to :company
end
class Company
has_many :users
end
Factory.define :user do |u|
u.name "Max"
u.association :company
end
Factory.define :user2, :parent => :user do |u|
u.name "Peter"
end
Factory.define :company do |c|
c.name "Acme Corporation"
end
How can I achieve having both users in the same company? When running the tests, FactoryGirl creates two company records but I want both users to be connected to one record.
Any hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类似的事情应该可以,但是请先阅读我的评论,我认为您对工厂女孩的想法是错误的。
Something like that should do, but please, read my comment first, I think you got the wrong idea about Factory Girl.
试试这个:
Try this: