Testing associations with factory girl

Associations are very important when we are working with relational databases. Writing relations in models is easy but when it comes testing them, it is a painful task.

Factory girl associations makes this easy. Before going into details, lets get the basics right.

What is Factory Girl?

   Factory_girl is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance. Read more of it here.

Testing Assocations

has_one association

Scenario : “user location” has one “email preference”

There are two ways to implement this.

  • You could define relation in user location factory.  First the factory creates the email preference object and then it creates user location object.
# spec/factories/user_location.rb

Factory.define :user_location , :class => UserLocation do |ul|
   ul.email_preference { |ep| ep.association(:user_location) }
end
  • You could alternatively define the same association in email preference factory. First the factory creates user location object and then it creates email preference object.
# spec/factories/email_preference.rb

Factory.define :email_preference , :class => EmailPreference do |ep|
   ep.association :user_location
end

has_many association

Scenario: “City” has many “deals”

Note, the factory  automatically creates the deal object and then it creates city object and associates the two.

# spec/factories/city.rb

Factory.define :city, :class => City do |c|
    c.deals { |d| [d.association(:deal)] }
end

has_many => through association

Scenario : “User” has many contests “through” ads

First the factory creates contest object and user object and then it creates ad object.

# spec/factories/ad.rb

Factory.define :ad, :class => Ad do |ad|
   ad.association :contest
   ad.association :user
end

  (or)

First the factory creates ‘user’ object and then it creates ‘ad’ object and then it creates ‘contest’ object.

# spec/factories/contest.rb

Factory.define :contest, :class => contest do |c|
   c.ads { |ad| [ad.association(:ad)] }
end

And in ‘ad’ factory

# spec/factories/ad.rb

Factory.define :ad, class => Ad do |ad|
   ad.association :user
end

Polymorphic relations

Among all relations defining polymorphic relations is a little bit complicated task.

Scenario : They are many type of ads like video, print, audio etc. So here video is one type of an ‘Ad’. First it creates ‘video’  object and then store resource type and ID in ‘ad’ object. Let’s see how we can define this in factories :

# spec/factories/ad.rb

Factory.define :ad, :class => Ad do |ad|
    ad.association :resource, :factory => video
end
I hope this was helpful. Feedback for this would be welcome. Let me know if there is any complex scenario you found which made testing difficult. I’d like to give that a shot!
Advertisements
This entry was posted in Ruby on Rails, Tutorials and tagged , , . Bookmark the permalink.

8 Responses to Testing associations with factory girl

  1. Dan says:

    factory_girl is awesome! nice post 😉

  2. Pingback: Factory_girl with NonActiveRecord classes « sivagollapallidotnet

  3. Pingback: Factory_girl with Non-ActiveRecord classes « Josh Software – Where Programming is an Art!

  4. eve says:

    Is it possible if we want object association with custom attribute?
    Let says, we have a factory :

    Factory.define(:user_company) do |uc|
    uc.association :user, :factory => :user
    uc.association :company, :factory => :company
    end

    So, we can call it:
    Factory(:user_company, :is_admin => true, :approved => true)

    Then we will get:
    eg. UserCompany :id => 1, :user_id => 1, :company_id => 1

    User :id => 1, :name => “user_1”
    Company :id => 1, :name => “company_1”

    But I don’t want the user with name “user_1”, I want to create the user with anything name..

    • Siva Gollapalli says:

      Yes you can do. In your case you have to do as following:

      uc.association :user, :factory => :user, :attributes => {:name => “user_2”}
      uc.association :company, :factory => :company, :attributes => {:name => “company_2” }

  5. Asnad says:

    what changes required if we have Scenario: ”City” embeds_many “deals”

    • When it comes to embeds_many we have to build the child object, assign to parent and then save as child object can’t exist without parent.

      your factory looks like

      #spec/factories/city.rb

      Factory.define :city, :class => City do |c|
      c.after_build {|c| c.deals = [Factory.build(:deal)]}
      end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s