views:

102

answers:

2

I have a Person model that has a many-to-many relationship with an Email model and I want to create a factory that lets me generate a first and last name for the person (this is already done) and create an email address that is based off of that person's name. Here is what I have for create a person's name:

Factory.sequence :first_name do |n|
  first_name = %w[FirstName1 FirstName2] # ... etc (I'm using a real subset of first names)
  first_name[(rand * first_name.length)]
end

Factory.sequence :last_name do |n|
  last_name = %w[LastName1 LastName2] # ... etc (I'm using a real subset of last names)
  last_name[(rand * last_name.length)]
end

Factory.define :person do |p|
  #p.id ???
  p.first_name { Factory.next(:first_name) }
  p.last_name { Factory.next(:last_name) }
  #ok here is where I'm stuck
  #p.email_addresses {|p| Factory(:email_address_person_link) }
end

Factory.define :email_address_person_link do |eapl|
  # how can I link this with :person and :email_address ? 
  # eapl.person_id ???
  # eapl.email_address_id ???
end

Factory.define :email_address do |e|
  #how can I pass p.first_name and p.last_name into here?
  #e.id ???
  e.email first_name + "." + last_name + "@test.com"
end
+1  A: 

Use a callback (see FG docs for more info). Callbacks get passed the current model being built.

Factory.define :person do |p|
  p.first_name { Factory.next(:first_name) }
  p.last_name { Factory.next(:last_name) }
  p.after_build { |m| p.email_addresses << "#{m.first_name}.#{m.last_name}@test.com" }
end

I think that works.

You could also save yourself some work by looking into using the Faker gem which creates realistic first and last names and e-mail addresses for you.

rspeicher
I don't think this is what I'm looking for exactly. I edited my question for clarity. +1 for the faker gem though. I would like to figure out how to do this though so I can better understand how factory_girl works.
DJTripleThreat
+1  A: 

Ok, I think I understand what you're asking now. Something like this should work (untested, but I've done something similar in another project):

Factory.define :person do |f|
  f.first_name 'John'
  f.last_name 'Doe'
end

Factory.define :email do |f|
end

# This is optional for isolating association testing; if you want this 
# everywhere, add the +after_build+ block to the :person factory definition
Factory.define :person_with_email, :parent => :person do |f|
  f.after_build do |p|
    p.emails << Factory(:email, :email => "#{p.first_name}.#{p.last_name}@gmail.com")
    # OR
    # Factory(:email, :person => p, :email => "#{p.first_name}.#{p.last_name}@gmail.com")
  end
end

As noted, using a third, separate factory is optional. In my case I didn't always want to generate the association for every test, so I made a separate factory that I only used in a few specific tests.

rspeicher
That works, you rock! And for anyone else who is interested, check out this blog too: http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-girl or http://railsondave.blogspot.com/2009/05/creating-hasmany-through-factories-with.html
DJTripleThreat