Factory Girl 与 has_many 关系出现错误

发布于 2024-09-29 05:52:50 字数 730 浏览 3 评论 0原文

我有以下工厂:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韵柒 2024-10-06 05:52:50

我建议不要将 has_many 关系数据添加到您的工厂中。原因是您的主要工厂现在依赖于填充此关联,并且如果关联发生变化,它会增加更多的耦合,并且可能会造成一些混乱。

如果你想测试这种关系(我建议你这样做),有一个很棒的宝石,名为 Shoulda< /a> 添加单元测试宏以确保关系设置正确。我还没有将它与内置的 Rails Test::Unit 一起使用,但 RSpec 示例看起来像这样:

describe Lead do
  it { should have_many(:emails) }
end

如果您确实想测试这种关系,则应该在规范中进行。从您的主要工厂中删除电子邮件关联并创建一个主要对象并尝试向其传递一些电子邮件对象,如下所示:

lead = Factory.build(:lead)
2.times do { lead.emails << Factory.build(:email, :lead => lead) }

然后它应该有几个电子邮件关联。然而,您应该对 ActiveRecord 有信心,并且只测试 Rails 已经为您所做的事情之外的事情。这就是应该发挥作用的地方。

我的另一条评论是关于您的电子邮件属于关系。由于您只是使用默认约定,rails 会知道要做什么。

class Email < ActiveRecord::Base
  belongs_to :lead
end

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:

describe Lead do
  it { should have_many(:emails) }
end

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:

lead = Factory.build(:lead)
2.times do { lead.emails << Factory.build(:email, :lead => lead) }

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.

class Email < ActiveRecord::Base
  belongs_to :lead
end
暖风昔人 2024-10-06 05:52:50

这是一篇有趣的文章,可能会有所帮助:

http://icelab。 com.au/articles/factorygirl-and-has-many-associations/

    FactoryGirl.define do
      factory :venue_with_gigs, :parent => :venue do
        after_create do |venue|
          FactoryGirl.create(:gig, :venue => venue)
        end
      end
    end

This is an interesting article which could be helpful:

http://icelab.com.au/articles/factorygirl-and-has-many-associations/

    FactoryGirl.define do
      factory :venue_with_gigs, :parent => :venue do
        after_create do |venue|
          FactoryGirl.create(:gig, :venue => venue)
        end
      end
    end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文