diff --git a/app/controllers/playlists_controller.rb b/app/controllers/playlists_controller.rb index 830ea84..0bba16c 100644 --- a/app/controllers/playlists_controller.rb +++ b/app/controllers/playlists_controller.rb @@ -22,7 +22,10 @@ class PlaylistsController < ApplicationController def update @playlist = Playlist.find(params[:id]) - @playlist.update_attributes params[:playlist] - redirect_to :action => 'index' + if @playlist.update_attributes params[:playlist] + redirect_to :action => 'index' + else + render :action => 'edit' + end end end diff --git a/spec/controllers/playlists_controller_spec.rb b/spec/controllers/playlists_controller_spec.rb index cce562e..49c5799 100644 --- a/spec/controllers/playlists_controller_spec.rb +++ b/spec/controllers/playlists_controller_spec.rb @@ -58,25 +58,37 @@ describe PlaylistsController do end describe 'PUT update' do - it 'updates the playlist' do - playlist = Factory.create(:playlist) - Playlist.any_instance.should_receive(:update_attributes). - with({'name' => 'Rock'}) - put :update, :id => playlist.id.to_s, :playlist => {:name => 'Rock'} + let (:playlist) { Factory.create(:playlist) } + + def do_update + put :update, :id => playlist.id.to_s, :playlist => { :name => 'Rock' } end - it 'assigns the requested playlist as @playlist' do - playlist = Factory.create(:playlist) - put :update, :id => playlist.id.to_s, :playlist => {:name => 'Rock'} - assigns[:playlist].should == playlist - end + context 'whith valid params' do + it 'updates the playlist' do + Playlist.any_instance.should_receive(:update_attributes). + with({'name' => 'Rock'}) + do_update + end - context 'when the playlist updates successfully' do it 'redirects to the playlists index' do - playlist = Factory.create(:playlist) - put :update, :id => playlist.id.to_s, :playlist => Factory.attributes_for(:playlist) + do_update response.should redirect_to(:action => 'index') end end + + context 'with invalid params' do + before { Playlist.any_instance.stub(:save).and_return(false) } + + it 'assigns the requested playlist as @playlist' do + do_update + assigns[:playlist].should == playlist + end + + it 'renders the edit template' do + do_update + response.should render_template('edit') + end + end end end