views:

68

answers:

1

I have written my basic models and defined their associations as well as the migrations to create the associated tables.

EDIT - Adding emphasis to what I specifically want to test.

I want to be able to test:

  • The associations are configured as intended
  • The table structures support the associations properly

I've written FG factories for all of my models in anticipation of having a complete set of test data but I can't grasp how to write a spec to test both belongs_to and has_many associations.

For example, given an Organization that has_many Users I want to be able to test that my sample Organization has a reference to my sample User.

Organization_Factory.rb:

Factory.define :boardofrec, :class => 'Organization' do |o|
  o.name 'Board of Recreation'
  o.address '115 Main Street'
  o.city 'Smallville'
  o.state 'New Jersey'
  o.zip '01929'
end

Factory.define :boardofrec_with_users, :parent => :boardofrec do |o|
  o.after_create do |org|
    org.users = [Factory.create(:johnny, :organization => org)]
  end
end

User_Factory.rb:

Factory.define :johnny, :class => 'User' do |u|
  u.name 'Johnny B. Badd'
  u.email '[email protected]'
  u.password 'password'
  u.org_admin true
  u.site_admin false
  u.association :organization, :factory => :boardofrec
end

Organization_spec.rb:

...
  it "should have the user Johnny B. Badd" do
    boardofrec_with_users = Factory.create(:boardofrec_with_users)
    boardofrec_with_users.users.should include(Factory.create(:johnny))
  end
...

This example fails because the Organization.users list and the comparison User :johnny are separate instances of the same Factory.

I realize this doesn't follow the BDD ideas behind what these plugins (FG, rspec) seemed to be geared for but seeing as this is my first rails application I'm uncomfortable moving forward without knowing that I've configured my associations and table structures properly.

+2  A: 

Your user factory already creates an organization by virtue of the Factory Girl association method:

it "should associate a user with an organization" do
  user = Factory.create(:johnny)
  user.organization.name.should == 'Board of Recreation'

  organization = user.organization
  organization.users.count.should == 1
end

Take a look at 'log/test.log' after running your spec -- you should see an INSERT for both the organization and the user.

If you wanted to test this without the Factory Girl association, make a factory that just creates the user and make the association in the spec:

it "should associate a user with an organization" do
  user = Factory.create(:johnny_no_org)
  org = Factory.create(:boardofrec)
  org.users.should be_empty
  org.users << user
  org.users.should include(user)
end

Of course all this is doing is testing whether ActiveRecord is doing its job. Since ActiveRecord is already thoroughly tested, you'll want to concentrate on testing the functionality of your application, once you've convinced yourself that the framework actually does what it's supposed to do.

zetetic
I changed the title and added some emphasis to the questions I'm looking to solve. I realize it is unnecessary to test ActiveRecord itself, but I'm looking to test whether **I've** implemented my associations properly and supported them properly with the appropriate table design.
Sugerman
Fair enough. Referring to my second example, I think a sufficient test of the associations would be `org.users.should include(user)` (the 'has_many` aspect) and `user.organization.should == org` (the 'belongs_to' aspect.
zetetic
Great, thanks. Going to give that a try.
Sugerman