Testing child model's create action

Hi all,

I have a model (stable) with many children (horses). I have created my
routes so that you access horses via stables, but this is causing
issues with trying to test my horses_controller create action.

Routes:
resources :users
resources :sessions, :only => [:new, :create, :destroy]
resources :stables, :only => [:new, :create, :show] do
resources :horses
end
resources :horses, :only => [:show]

root :to => ‘pages#home’

Horses controller:
def create
@horse =
Stable.find_by_id(params[:stable_id]).horses.build(params[:horse])
if @horse.save
flash[:success] = ‘Horse created!’
redirect_to horse_path(@horse)
else
@breeds = Breed.all
@genders = [[“Gelding”, “G”],
[“Mare”, “M”],
[“Stallion”, “S”]
]
@title = ‘Create A Horse’
render :new
end
end

Stables controller spec:
describe “POST horses ‘create’” do
before(:each) do
@user = Factory(:user)
@stable = Factory(:stable, :user => @user)
end

describe "as a signed-in user" do
  before(:each) do
    test_sign_in(@user)
  end

  describe "failure" do
    before(:each) do
      @attr = { :name => '', :gender => '', :owner =>

@stable, :breed_id => ‘’ }
end

    it "should not create a stable" do
      lambda do
        {:post => 'stable_horses', :horse => @attr, :stable =>

@stable}
end.should_not change(Horse, :count)
end

    # fails, error 1 below
    it "should render the 'new' page" do
      {:post => 'stable_horses', :horse => @attr, :stable =>

@stable}.
should render_template(‘stable/horses’)
end
end

  describe "success" do
    before(:each) do
      @breed = Breed.new(:name => 'Thoroughbred')
      @attr = {
        :name => 'Bob',
        :gender => 'G',
        :owner => @stable,
        :breed_id => @breed.id }
    end

    # fails, error 2 below
    it "should create a horse" do
      lambda do
        {:post => 'stable_horses', :horse => @attr, :stable_id =>

@stable}
end.should change(Horse, :count).by(1)
end

    # fails, error 3 below
    it "should redirect to the horse page" do
      {:post => 'stable_horses', :horse => @attr, :stable_id =>

@stable}
response.should redirect_to(horse_path(assigns(:horse)))
end

    # fails, error 4 below
    it "should have a flash message" do
      {:post => 'stable_horses', :horse => @attr, :stable_id =>

@stable}
flash[:success].should =~ /horse created/i
end

  end
end

end

Errors when tests are run:

  1. StablesController POST horses ‘create’ as a signed-in user
    failure should render the ‘new’ page
    Failure/Error: {:post => ‘stable_horses’, :horse =>
    @attr, :stable => @stable}.
    expecting <“stable/horses”> but rendering with <"">.
    Expected block to return true value.

    ./spec/controllers/stables_controller_spec.rb:261:in `block (5

levels) in <top (required)>’

  1. StablesController POST horses ‘create’ as a signed-in user
    success should create a horse
    Failure/Error: lambda do
    count should have been changed by 1, but was changed by 0

    ./spec/controllers/stables_controller_spec.rb:277:in `block (5

levels) in <top (required)>’

  1. StablesController POST horses ‘create’ as a signed-in user
    success should redirect to the horse page
    Failure/Error: response.should
    redirect_to(horse_path(assigns(:horse)))
    ActionController::RoutingError:
    No route matches {:action=>“show”, :controller=>“horses”}

    ./spec/controllers/stables_controller_spec.rb:284:in `block (5

levels) in <top (required)>’

  1. StablesController POST horses ‘create’ as a signed-in user
    success should have a flash message
    Failure/Error: flash[:success].should =~ /horse created/i
    expected: /horse created/i
    got: nil (using =~)

    ./spec/controllers/stables_controller_spec.rb:289:in `block (5

levels) in <top (required)>’

I’m sure I’m missing something pretty basic, but am not sure what it
is. Thanks!