Topic: rspec has many through issue

This may be an easy fix, but I've been struggling to figure it out.

Pretty straightforward has many through code:

school
user
schools_users

The functionality in the app is working great with the associated table. It is the testing that is failing for just the show and destroy tests. Create, update, index are all successful.

Factories:

Factory.define :user do |user|
  user.first_name             "John"
  user.last_name              "Doe"
  user.email                  "user@user.com"
  user.password               "password"
  user.password_confirmation  "password"
end

Factory.define :school do |school|
  school.name             "School Name"
  school.address          "1234 Winding Way"
end

Factory.define :schools_user do |u|
  u.user        { |c| c.association(:user) }
  u.school      { |c| c.association(:school) }
  u.user_role    'user,admin'
end

Failing test:

describe SchoolsController do
  before(:each) do
    sign_in :user, Factory(:user)
    @school = Factory(:school)
  end
  render_views

  context '#show' do
    it "should render show template" do
      get :show, :id => @school
      response.should be_success
    end
  end
end

Failure message:

Failures:

  1) SchoolsController#show should render show template
     Failure/Error: get :show, :id => @school
     ActiveRecord::RecordNotFound:
       Couldn't find School with ID=980190976 [WHERE (("schools_users".user_id = 138))]
     # ./app/controllers/schools_controller.rb:9:in `show'
     # ./spec/controllers/schools_controller_spec.rb:12:in `block (3 levels) in <top (required)>'

Any help is appreciated!

Re: rspec has many through issue

Solved this by creating the association.

@school = @user.schools.create!(Factory(:school))

in place of

@school = Factory(:school)