2012-04-01 17 views
5

Esta es una tarea para la que estoy trabajando para familiarizarme con TDD y Rspec. Pero de alguna manera No entiendo por qué la siguiente prueba falló:Rspec, ¿la prueba del controlador de actualización no funciona?

describe 'update' do 
    fixtures :movies 
    before :each do 
     @fake_movie = movies(:star_wars_movie) 
    end 
    it 'should retrieve the right movie from Movie model to update' do 
     Movie.should_receive(:find).with(@fake_movie.id.to_s).and_return(@fake_movie) 
     put :update, :id => @fake_movie.id, :movie => {:rating => @fake_movie.rating} 
    end 

    it 'should prepare the movie object available for update' do 
     put :update, :id => @fake_movie.id, :movie => {:rating => @fake_movie.rating} 
     assigns(:movie).should == @fake_movie 
    end 

    it 'should pass movie object the new attribute value to updated' do 
     fake_new_rating = 'PG-15' 
     @fake_movie.stub(:update_attributes!).with("rating" => fake_new_rating).and_return(:true) 
     put :update, :id => @fake_movie.id, :movie => {:rating => fake_new_rating} 
     @fake_movie.should_receive(:update_attributes!).with("rating" => fake_new_rating).and_return(:true) 
    end 
    end 

El mensaje de error que tengo es:

Failures: 

    1) MoviesController update should pass movie object the new attribute value to updated 
    Failure/Error: @fake_movie.should_receive(:update_attributes!).with("rating" => fake_new_rating).and_return(:true) 
     (#<Movie:0xd39ea38>).update_attributes!({"rating"=>"PG-15"}) 
      expected: 1 time 
      received: 0 times 
    # ./spec/controllers/movies_controller_spec.rb:99:in `block (3 levels) in <top (required)>' 

Finished in 0.60219 seconds 
12 examples, 1 failure 

Failed examples: 

rspec ./spec/controllers/movies_controller_spec.rb:95 # MoviesController update should pass movie object the new attribute value to updated 

Básicamente se dice que mi última línea de prueba falló @fake_movie.should_receive(:update_attributes!).with("rating" => fake_new_rating).and_return(:true), y creo no recibió la llamada a la función '¡atributos_actualización!' en absoluto, pero ¿por qué?

Y el código del controlador:

def update 
    @movie = Movie.find params[:id] 
    @movie.update_attributes!(params[:movie]) 
    flash[:notice] = "#{@movie.title} was successfully updated." 
    redirect_to movie_path(@movie) 
    end 

Gracias de antemano

Respuesta

3

debe ser:

it 'should pass movie object the new attribute value to updated' do 
    fake_new_rating = 'PG-15' 
    Movie.stub(:find).and_return(@fake_movie) 
    @fake_movie.should_receive(:update_attributes!).with("rating" => fake_new_rating).and_return(:true) 
    put :update, :id => @fake_movie.id, :movie => {:rating => fake_new_rating} 
end 

De lo contrario, la línea sería @movie = Movie.find params[:id] consulta en el modelo.

+2

Como un buen apéndice de la práctica solo lo que posee, no posee 'find' o' update_attributes' usted no debe untarlos – Calin

Cuestiones relacionadas