2012-04-01 3 views
5

이것은 TDD 및 Rspec에 익숙해지기 위해 노력하고있는 숙제입니다. 내가 가진Rspec, 업데이트 컨트롤러 테스트가 작동하지 않습니까?

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 

오류 메시지는 다음과 같습니다 :하지만 어떻게 든 다음 테스트가 실패한 이유를 이해가 안

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 

기본적으로는 테스트의 마지막 라인이 @fake_movie.should_receive(:update_attributes!).with("rating" => fake_new_rating).and_return(:true)을 실패라고, 나는 생각한다 함수 호출 'update_attributes!'를 전혀받지 못했지만 그 이유는 무엇입니까?

그리고 컨트롤러 코드 : 사전에

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 

감사

답변

3

가되어야한다

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 

그렇지 않으면, 라인 @movie = Movie.find params[:id] 모델에 대해 쿼리합니다.

+2

당신이 소유 한 것만 좋은 연습 스텁으로, 당신은'find' 또는'update_attributes'를 소유하지 않습니다. – Calin

관련 문제