Cloud Computing: The Digital Shift 2.0 – Understanding the Current Market Trends

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.
[sourcecode lang=”ruby”]
# spec/factories/user_location.rb

Factory.define :user_location , :class => UserLocation do |ul|
ul.email_preference { |ep| ep.association(:user_location) }

  • 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.

[sourcecode lang=”ruby”]
# spec/factories/email_preference.rb

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

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.

[sourcecode lang=”ruby”]
# spec/factories/city.rb

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

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.

[sourcecode lang=”ruby”]
# spec/factories/ad.rb

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


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

[sourcecode lang=”ruby”]
# spec/factories/contest.rb

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

And in ‘ad’ factory

[sourcecode lang=”ruby”]
# spec/factories/ad.rb

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

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 :

[sourcecode lang=”ruby”]
# spec/factories/ad.rb

Factory.define :ad, :class => Ad do |ad|
ad.association :resource, :factory => video

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!

0 thoughts on “Testing associations with factory girl”

  1. Pingback: Factory_girl with NonActiveRecord classes « sivagollapallidotnet

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

  3. 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

    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..

    1. 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” }

    1. 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


      Factory.define :city, :class => City do |c|
      c.after_build {|c| = []}

  4. FactoryBot.define do
    factory :company, :class => Company do |c|

    c.vendors { |v| [v.association(:vendor)] } {‘Demo Changed name’}
    c.contact_first {‘Cary’}
    c.contact_last {‘Pennington’}
    c.phone1 {‘5555555555’}
    c.phone2 {‘(222) 222-2222’} {‘’}
    c.tin {nil}
    c.created_at {‘2019-03-19 04:37:59’}
    c.updated_at {‘2019-06-26 17:10:03’}
    c.deleted_at {nil}
    c.delivery_method {‘Electronic’}

    association :account, factory: :account


    In this company with has_many vendors relationship facing systemstack error stack level too deep ? why?

Leave a Comment

Your email address will not be published.