如何在 Factory Girl 中创建 has_and_belongs_to_many 关联
鉴于以下情况,
class User < ActiveRecord::Base
has_and_belongs_to_many :companies
end
class Company < ActiveRecord::Base
has_and_belongs_to_many :users
end
您如何为公司和用户定义工厂(包括双向关联)?这是我的尝试,
Factory.define :company do |f|
f.users{ |users| [users.association :company]}
end
Factory.define :user do |f|
f.companies{ |companies| [companies.association :user]}
end
现在我尝试
Factory :user
也许毫不奇怪,这会导致无限循环,因为工厂递归地相互使用彼此来定义自己。
更令人惊讶的是,我没有在任何地方找到如何做到这一点的提及,是否有定义必要工厂的模式,或者我正在做一些根本错误的事情?
Given the following
class User < ActiveRecord::Base
has_and_belongs_to_many :companies
end
class Company < ActiveRecord::Base
has_and_belongs_to_many :users
end
how do you define factories for companies and users including the bidirectional association? Here's my attempt
Factory.define :company do |f|
f.users{ |users| [users.association :company]}
end
Factory.define :user do |f|
f.companies{ |companies| [companies.association :user]}
end
now I try
Factory :user
Perhaps unsurprisingly this results in an infinite loop as the factories recursively use each other to define themselves.
More surprisingly I haven't found a mention of how to do this anywhere, is there a pattern for defining the necessary factories or I am doing something fundamentally wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这是适合我的解决方案。
如果您需要特定的公司,您可以通过这种方式使用工厂
希望这对某人有帮助。
Here is the solution that works for me.
if you will need specific company you can use factory this way
Hope this will be helpful for somebody.
Factorygirl 此后已进行更新,现在包含回调来解决此问题。看看 http://robots.thoughtbot.com/post /254496652/aint-no-calla-back-girl 了解更多信息。
Factorygirl has since been updated and now includes callbacks to solve this problem. Take a look at http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-girl for more info.
在我看来,只需创建两个不同的工厂,例如:
当您为用户编写测试用例时,只需像这样编写
希望它会起作用。
In my opinion, Just create two different factories like:
When you write the test-cases for user then just write like this
Hope it will work.
我在提供的网站上找不到上述案例的示例。 (只有 1:N 和多态关联,但没有 habtm)。我有一个类似的案例,我的代码如下所示:
I couldn´t find an example for the above mentioned case on the provided website. (Only 1:N and polymorphic assocations, but no habtm). I had a similar case and my code looks like this:
对我有用的是在使用工厂时设置关联。
使用你的例子:
What worked for me was setting the association when using the factory.
Using your example:
发现这种方式很好而且很详细:
Found this way nice and verbose:
警告:将用户:[user]更改为:users => [用户] 对于 ruby 1.8.x
Warning: Change users: [user] to :users => [user] for ruby 1.8.x
对于 HABTM,我使用了特征和回调。
假设您有以下模型:
您可以定义上面的工厂:
For HABTM I used traits and callbacks.
Say you have the following models:
You can define the Factory above:
首先,我强烈鼓励您使用 has_many :through 而不是 habtm (有关此的更多信息 这里),所以你最终会得到类似的结果:
在此之后,双方都会有 has_many 关联,并且可以按照你所做的方式在factory_girl 中分配给它们。
First of all I strongly encourage you to use has_many :through instead of habtm (more about this here), so you'll end up with something like:
After this you'll have has_many association on both sides and can assign to them in factory_girl in the way you did it.
Rails 5 更新:
您应该考虑:
has_many :through
关联,而不是使用has_and_belongs_to_many
关联。该关联的用户工厂如下所示:
您可以以类似的方式创建公司工厂。
设置两个工厂后,您可以使用
companies_count 选项
创建user_with_companies
工厂。在这里您可以指定用户所属的公司数量:create(:user_with_companies,companys_count: 15)
您可以找到有关这里是工厂女孩协会。
Update for Rails 5:
Instead of using
has_and_belongs_to_many
association, you should consider:has_many :through
association.The user factory for this association looks like this:
You can create the company factory in a similar way.
Once both factories are set, you can create
user_with_companies
factory withcompanies_count option
. Here you can specify how many companies the user belongs to:create(:user_with_companies, companies_count: 15)
You can find detailed explanation about factory girl associations here.
您可以定义新工厂并使用 after(:create) 回调来创建关联列表。让我们看看如何在此示例中执行此操作:
属性companys_count是瞬态的,可在工厂的属性中以及通过评估器的回调中使用。现在,您可以创建一个包含公司的用户,并可以选择指定您想要的公司数量:
You can define new factory and use after(:create) callback to create a list of associations. Let's see how to do it in this example:
Attribute companies_count is a transient and available in attributes of the factory and in the callback via the evaluator. Now, you can create a user with companies with the option to specify how many companies you want: