Factory Girl 与 has_many 关系出现错误
我有以下工厂:
Factory.define :email do |email|
email.email {"infomcburney.cowan.com"}
end
Factory.define :lead do |lead|
lead.emails {|emails| [emails.association(:email)]}
end
正在对以下类进行建模
class Lead < ActiveRecord::Base
has_many :emails
end
class Email < ActiveRecord::Base
belongs_to :lead, :class_name => "Lead", :foreign_key => "lead_id"
end
当我通过 shoulda: 运行此测试时,
should "capture emails" do
lead = Factory.build(:lead)
assert_equal(1, lead.emails.size)
end
出现以下错误:
工厂::属性定义错误: 属性已定义:电子邮件
我完全陷入困境,任何人都可以指出我正确的方向。我正在使用factory_girl 1.3.2。
I have the following factories:
Factory.define :email do |email|
email.email {"infomcburney.cowan.com"}
end
Factory.define :lead do |lead|
lead.emails {|emails| [emails.association(:email)]}
end
Which are modeling the following classes
class Lead < ActiveRecord::Base
has_many :emails
end
class Email < ActiveRecord::Base
belongs_to :lead, :class_name => "Lead", :foreign_key => "lead_id"
end
When I run the this test through shoulda:
should "capture emails" do
lead = Factory.build(:lead)
assert_equal(1, lead.emails.size)
end
I get the following error:
Factory::AttributeDefinitionError:
Attribute already defined: emails
I am completely stuck on this, can anyone point me in the right direction. I am using factory_girl 1.3.2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议不要将 has_many 关系数据添加到您的工厂中。原因是您的主要工厂现在依赖于填充此关联,并且如果关联发生变化,它会增加更多的耦合,并且可能会造成一些混乱。
如果你想测试这种关系(我建议你这样做),有一个很棒的宝石,名为 Shoulda< /a> 添加单元测试宏以确保关系设置正确。我还没有将它与内置的 Rails Test::Unit 一起使用,但 RSpec 示例看起来像这样:
如果您确实想测试这种关系,则应该在规范中进行。从您的主要工厂中删除电子邮件关联并创建一个主要对象并尝试向其传递一些电子邮件对象,如下所示:
然后它应该有几个电子邮件关联。然而,您应该对 ActiveRecord 有信心,并且只测试 Rails 已经为您所做的事情之外的事情。这就是应该发挥作用的地方。
我的另一条评论是关于您的电子邮件属于关系。由于您只是使用默认约定,rails 会知道要做什么。
I would recommend against adding has_many relationship data to your factories. The reason for this is that your lead factory now depends on populating this association and it's adding more coupling and potentially some confusion down the road if the association changes.
If you want to test this relationship (and I recommend you do), there's a great gem called Shoulda that adds unit test macros to ensure that the relationships are setup right. I haven't used it with the built in Rails Test::Unit, but an RSpec example would look something like:
If you really want to test this relationship, you should do it in the spec. Remove the emails association from your lead factory and create a lead object and try to pass it a few email objects like so:
Then it should have a couple emails association with it. However, you should put some faith in ActiveRecord and just test things that are above and beyond what Rails is already doing for you. This is where Shoulda comes in.
Another comment I have is on your Email belongs_to relationship. Since you're just using the default conventions, rails will know what to do.
这是一篇有趣的文章,可能会有所帮助:
http://icelab。 com.au/articles/factorygirl-and-has-many-associations/
This is an interesting article which could be helpful:
http://icelab.com.au/articles/factorygirl-and-has-many-associations/