2010-07-02 2 views
1

저는 rspec을 사용하는 데있어서 매우 익숙하며 컨트롤러에 대한 테스트를 작성하려고합니다. 나는이 같은이 작업을 테스트하기 위해 노력하고컨트롤러 및 스텁에 대한 RSpec

class CardsController < ApplicationController 
    before_filter :require_user 

    def show 
    @cardset = current_user.cardsets.find_by_id(params[:cardset_id]) 

    if @cardset.nil? 
     flash[:notice] = "That card doesn't exist. Try again." 
     redirect_to(cardsets_path) 
    else 
     @card = @cardset.cards.find_by_id(params[:id]) 
    end 
    end 
end 

: 나는 (내가 스텁에 대한 모카를 사용하고 있습니다)이 컨트롤러가

describe CardsController, "for a logged in user" do 
    before(:each) do 
    @cardset = Factory(:cardset) 
    profile = @cardset.profile 
    controller.stub!(:current_user).and_return(profile) 
    end 

    context "and created card" do 
    before(:each) do 
     @card = Factory(:card) 
    end 

    context "with get to show" do 
     before(:each) do 
     get :show, :cardset_id => @cardset.id, :id => @card.id 
     end 

     context "with valid cardset" do 
     before(:each) do 
      Cardset.any_instance.stubs(:find).returns(@cardset) 
     end 

     it "should assign card" do 
      assigns[:card].should_not be_nil 
     end 

     it "should assign cardset" do 
      assigns[:cardset].should_not be_nil 
     end 

     end 
    end 
    end 
end 

은 "cardset을 지정해야합니다"테스트를 통과를하지만 이 라인을 올바르게 스터핑하는 방법을 알아낼 수 없습니다. @card = @cardset.cards.find_by_id(params[:id]) "카드를 지정해야합니다"테스트. 이 동작을 테스트하는 가장 좋은 방법은 무엇입니까? 아니면 올바른 추적을하고 있다면 모델 호출을 올바르게 스텁하는 방법은 무엇입니까?

답변

0

나는이

Cardset.stubs(:find_by_id).returns(@cardset) 
@cardset.cards.stubs(:find_by_id).returns(@card) 
+0

RSpec 2.8에서는'stubs' 메서드가 작동하지 않지만'stub'은 있습니다. 이 문제를 가지고있는 다른 사람은이 개정판을 시도 할 수 있습니다 :'Cardset.stub (: find) .and_return (@cardset)'. – evanrmurphy

0

Ok, 이전의 대답이 잘못되었습니다.

먼저 : find이 아닌 find_by_id을 스터 빙하고 있습니다. find_by_id는 find의 기본값이므로 find_by_id를 사용할 필요는 없습니다. before :each 주문은 Cardset

셋째 스터브 전에 get :show를 호출하는 것입니다 : 그래서 find

둘째 사용하여 test.log를 확인하고 재 처리되지 않습니다 있는지 확인하십시오. current_user이 설정되기 전에 require_user 작업으로 인해 리디렉션이 발생할 수 있습니다.

class CardsController < ApplicationController 
    ... 
    @card = @cardset.cards.find(params[:id]) 
    ... 
end 

describe CardsController, "for a logged in user" do 
    before(:each) do 
    @cardset = Factory(:cardset) 
    profile = @cardset.profile 
    controller.stub!(:current_user).and_return(profile) 
    end 

    context "and created card" do 
    before(:each) do 
     @card = Factory(:card) 
    end 

    context "with get to show" do 

     context "with valid cardset" do 
     before(:each) do 
      Cardset.any_instance.stubs(:find).returns(@cardset) 
      get :show, :cardset_id => @cardset.id, :id => @card.id 
     end 

     it "should assign card" do 
      assigns[:card].should_not be_nil 
     end 

     it "should assign cardset" do 
      assigns[:cardset].should_not be_nil 
     end 

     end 
    end 
    end 
end 
+0

좋아 덕분에, 나는이 일을 확인합니다 곳을 찾고 결국 스텁. find_by_id를 사용하는 이유는 레코드가 발견되지 않으면 예외를 throw하는 대신 nil을 반환한다는 것이므로이 상황에서 다루기가 더 쉬워 보입니다. – trobrock

+0

여기에 표시된 내용으로 코드를 업데이트했습니다. http://gist.github.com/461667'before_filter'에 의해 리디렉션되었지만 여전히 "카드를 할당해야합니다"라는 테스트가 실패했습니다. – trobrock

관련 문제