Factory Girl error with has_many relationship

2019-04-23 16:55发布

问题:

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.

回答1:

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


回答2:

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