Topic: rspec nested resource controller

Can anyone help by giving an example controller spec for nested resource, or help by fixing my code. I get 16 no route match errors, how do you work out the routes?

Thanks.


class GigsController < ApplicationController
    before_filter :get_artist
   
  def index
    @gigs = @artist.gigs
  end

  def show
    @gig = @artist.gigs.find(params[:id])
  end

  def new  
    @gig = @artist.gigs.new
  end

  def edit
    @gig = Gig.find(params[:id])
  end

  def create
    @gig = @artist.gigs.build(params[:gig])
      if @gig.save
        flash[:notice] = 'Gig was successfully created.'
       redirect_to ([@artist, @gig]) 
      else
     render :action => "new"
    end
  end

  def update
    @gig = @artist.gigs.find(params[:id])
      if @gig.update_attributes(params[:gig])
        flash[:notice] = 'Gig was successfully updated.'
    redirect_to ([@artist, @gig]) 
      else
        render :action => "edit" 
    end
  end

  def destroy
    @gig = @artist.gigs.find(params[:id])
    @gig.destroy
    flash[:notice] = 'Gig was successfully destroyed.'
    redirect_to(artist_gigs_url(@artist))
  end
  

end
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe GigsController do

  def mock_gig(stubs={})
    @mock_gig ||= mock_model(Gig, stubs)
  end

  describe "GET index" do
    it "assigns all gigs as @gigs" do
      Gig.stub!(:find).with(:all).and_return([mock_gig])
      get :index
      assigns[:gigs].should == [mock_gig]
    end
  end

  describe "GET show" do
    it "assigns the requested gig as @gig" do
      Gig.stub!(:find).with("37").and_return(mock_gig)
      get :show, :id => "37"
      assigns[:gig].should equal(mock_gig)
    end
  end

  describe "GET new" do
    it "assigns a new gig as @gig" do
      Gig.stub!(:new).and_return(mock_gig)
      get :new
      assigns[:gig].should equal(mock_gig)
    end
  end

  describe "GET edit" do
    it "assigns the requested gig as @gig" do
      Gig.stub!(:find).with("37").and_return(mock_gig)
      get :edit, :id => "37"
      assigns[:gig].should equal(mock_gig)
    end
  end

  describe "POST create" do

    describe "with valid params" do
      it "assigns a newly created gig as @gig" do
        Gig.stub!(:new).with({'these' => 'params'}).and_return(mock_gig(:save => true))
        post :create, :gig => {:these => 'params'}
        assigns[:gig].should equal(mock_gig)
      end

      it "redirects to the created gig" do
        Gig.stub!(:new).and_return(mock_gig(:save => true))
        post :create, :gig => {}
        response.should redirect_to(gig_url(mock_gig))
      end
    end

    describe "with invalid params" do
      it "assigns a newly created but unsaved gig as @gig" do
        Gig.stub!(:new).with({'these' => 'params'}).and_return(mock_gig(:save => false))
        post :create, :gig => {:these => 'params'}
        assigns[:gig].should equal(mock_gig)
      end

      it "re-renders the 'new' template" do
        Gig.stub!(:new).and_return(mock_gig(:save => false))
        post :create, :gig => {}
        response.should render_template('new')
      end
    end

  end

  describe "PUT update" do

    describe "with valid params" do
      it "updates the requested gig" do
        Gig.should_receive(:find).with("37").and_return(mock_gig)
        mock_gig.should_receive(:update_attributes).with({'these' => 'params'})
        put :update, :id => "37", :gig => {:these => 'params'}
      end

      it "assigns the requested gig as @gig" do
        Gig.stub!(:find).and_return(mock_gig(:update_attributes => true))
        put :update, :id => "1"
        assigns[:gig].should equal(mock_gig)
      end

      it "redirects to the gig" do
        Gig.stub!(:find).and_return(mock_gig(:update_attributes => true))
        put :update, :id => "1"
        response.should redirect_to(gig_url(mock_gig))
      end
    end

    describe "with invalid params" do
      it "updates the requested gig" do
        Gig.should_receive(:find).with("37").and_return(mock_gig)
        mock_gig.should_receive(:update_attributes).with({'these' => 'params'})
        put :update, :id => "37", :gig => {:these => 'params'}
      end

      it "assigns the gig as @gig" do
        Gig.stub!(:find).and_return(mock_gig(:update_attributes => false))
        put :update, :id => "1"
        assigns[:gig].should equal(mock_gig)
      end

      it "re-renders the 'edit' template" do
        Gig.stub!(:find).and_return(mock_gig(:update_attributes => false))
        put :update, :id => "1"
        response.should render_template('edit')
      end
    end

  end

  describe "DELETE destroy" do
    it "destroys the requested gig" do
      Gig.should_receive(:find).with("37").and_return(mock_gig)
      mock_gig.should_receive(:destroy)
      delete :destroy, :id => "37"
    end

    it "redirects to the gigs list" do
      Gig.stub!(:find).and_return(mock_gig(:destroy => true))
      delete :destroy, :id => "1"
      response.should redirect_to(gigs_url)
    end
  end

end
(in /Users/kevin/projects/excentric1)
    artist_gigs GET    /artists/:artist_id/gigs(.:format)          {:controller=>"gigs", :action=>"index"}
                POST   /artists/:artist_id/gigs(.:format)          {:controller=>"gigs", :action=>"create"}
 new_artist_gig GET    /artists/:artist_id/gigs/new(.:format)      {:controller=>"gigs", :action=>"new"}
edit_artist_gig GET    /artists/:artist_id/gigs/:id/edit(.:format) {:controller=>"gigs", :action=>"edit"}
     artist_gig GET    /artists/:artist_id/gigs/:id(.:format)      {:controller=>"gigs", :action=>"show"}
                PUT    /artists/:artist_id/gigs/:id(.:format)      {:controller=>"gigs", :action=>"update"}
                DELETE /artists/:artist_id/gigs/:id(.:format)      {:controller=>"gigs", :action=>"destroy"}
        artists GET    /artists(.:format)                          {:controller=>"artists", :action=>"index"}
                POST   /artists(.:format)                          {:controller=>"artists", :action=>"create"}
     new_artist GET    /artists/new(.:format)                      {:controller=>"artists", :action=>"new"}
    edit_artist GET    /artists/:id/edit(.:format)                 {:controller=>"artists", :action=>"edit"}
         artist GET    /artists/:id(.:format)                      {:controller=>"artists", :action=>"show"}
                PUT    /artists/:id(.:format)                      {:controller=>"artists", :action=>"update"}
                DELETE /artists/:id(.:format)                      {:controller=>"artists", :action=>"destroy"}
           root GET    /                                           {:controller=>"artists", :action=>"index"}
                       /:artist_id/:controller/:action/:id         

Re: rspec nested resource controller

depassion wrote:
class GigsController < ApplicationController
    before_filter :get_artist
   
  def index
    @gigs = @artist.gigs
  end
end
describe GigsController do

  def mock_gig(stubs={})
    @mock_gig ||= mock_model(Gig, stubs)
  end

  describe "GET index" do
    it "assigns all gigs as @gigs" do
      Gig.stub!(:find).with(:all).and_return([mock_gig])
      get :index
      assigns[:gigs].should == [mock_gig]
    end
  end
end

There are a couple of things wrong with this, one I understand how to fix and one I don't.

First, when you do the get :index, you have to pass an artist id, or the controller won't be able to load up an artist.

Second, your test doesn't test what is happening I don't think.   You are stubbing (:find) for Gigs, but in the controller, in reality you are not searching for all gigs, you are searching for all Gigs with the specific artist's id.

So maybe something like

@artist = mock_model(Artist)

Gig.stub!(:find).with(:artist_id => @artist.id).and_return([mock_gig])
get :index
Gig.should_receive(:find).with(:artist_id => @artist.id)
assigns[:gigs].should == [mock_gig]

Would be a much better test.

Hope this helps even if it is very late, or at the least help someone in the future.

Joe

Joseph DelCioppio
Synantus Designs