Fix playlist/update action behaviour with invalid params

This commit is contained in:
Thibault Jouan 2011-08-24 20:20:39 +00:00
parent 7b2b594d7d
commit c023a071d0
2 changed files with 30 additions and 15 deletions

View File

@ -22,7 +22,10 @@ class PlaylistsController < ApplicationController
def update
@playlist = Playlist.find(params[:id])
@playlist.update_attributes params[:playlist]
if @playlist.update_attributes params[:playlist]
redirect_to :action => 'index'
else
render :action => 'edit'
end
end
end

View File

@ -58,25 +58,37 @@ describe PlaylistsController do
end
describe 'PUT update' do
let (:playlist) { Factory.create(:playlist) }
def do_update
put :update, :id => playlist.id.to_s, :playlist => { :name => 'Rock' }
end
context 'whith valid params' 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'}
do_update
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 '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