Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8851181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:05:27+00:00 2026-06-14T13:05:27+00:00

I have been using rails for almost 4 years now in in all that

  • 0

I have been using rails for almost 4 years now in in all that time I have been sinning. I have never written a single test. Not sure why it has taken me this long to see the giant mistake I have been making but I have now. I want to change my development around and start utilizing TDD. But to do that I have to build up a test suit for the application I am currently working on. I have got rspec and factory_girl setup and am starting to understand things abit. I have some fairly complex models I am trying to test and I am stuck. Here is what I have:

class BusinessEntity
  has_many :business_locations

class BusinessLocation
   belongs_to :business_entity
   has_many :business_contacts

   validates :business_entity_id, :presence => true

class BusinessContact
   belongs_to :business_location
   has_many :business_phones

   validates :business_location_id, :presence => true

class BusinessPhone
    belongs_to :business_contact

    validates :business_contact_id, :presence => true

There is more going on in these models but this is what I am stuck on. How can I create a factory for business_entity that builds all required children? So in the spec file I can just FactoryGirl.create(:business_entity) and be able to use this for other model testing.
I have this factory

    require 'faker'

FactoryGirl.define do
  factory :business_entity do
    name "DaveHahnDev"        
  end

  factory :business_location do
    name "Main Office"
    business_entity
    address1 "139 fittons road west"
    address2 "a different address"
    city { Faker::Address.city }
    province "Ontario"
    country "Canada"
    postal_code "L3V3V3"
  end

  factory :business_contact do
    first_name { Faker::Name.first_name}
    last_name { Faker::Name.last_name}
    business_location
    email { Faker::Internet.email}
  end

  factory :business_phone do
    name { Faker::PhoneNumber.phone_number}
    business_contact
    number_type "Work"
  end
end

This passes this

require 'spec_helper'


  it "has a valid factory" do
    FactoryGirl.build(:business_entity).should be_valid
  end

So how can I use this factory to create the business_entity with all children for use in other spec tests.

I hope this is clear enough and any help would be greatly appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:05:28+00:00Added an answer on June 14, 2026 at 1:05 pm

    If I’ve understood correctly you need to create associations. The most basic way to do this using FactoryGirls is just to add the factory name in another factory block. So, in your case it will be the following:

    # factories.rb
    
    FactoryGirl.define do
      factory :business_entity do
        name "DaveHahnDev"        
      end
    
      factory :business_location do
        business_entity # this automatically creates an association
        name "Main Office"
        business_entity
        address1 "139 fittons road west"
        address2 "a different address"
        city { Faker::Address.city }
        province "Ontario"
        country "Canada"
        postal_code "L3V3V3"
      end
    
      factory :business_contact do
        business_location
        first_name { Faker::Name.first_name}
        last_name { Faker::Name.last_name}
        business_location
        email { Faker::Internet.email}
      end
    
      factory :business_phone do
        business_contact
        name { Faker::PhoneNumber.phone_number}
        business_contact
        number_type "Work"
      end
    end
    

    After adding these lines you can call FactoryGirl.create(:business_location), which will create a new BussinessLocation record, BussinessEntity record and associate them.

    For more detailed information check FactoryGirls Wiki – Associations

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using mysql in a new rails application, but now I wanted
I have been using Grails for some time now, but in school they are
I have been using Rails for over 4 years so obviously I like Rails
we have been using rails 3.0.9 for a long time, because of dependencies we
I have been using MSSQL 2005 with Rails for quite a while now, and
I have been using ActiveResource in my Ruby on Rails applications for some time,
I have been using the Rails console a fair bit lately and its making
I have been using TortoiseSVN for some time and I really like it. I
We are using Rails 2.3.5 and have been experiencing seemingly random Timeout::Error: execution expired
I have been working on rails project, which had been made using noSql(Mongoid). Every

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.