2011-02-28 4 views
14

이전 필터로 사용할 응용 프로그램 컨트롤러에서 메서드를 테스트하려고합니다. 이렇게하려면 제대로 작동하는지 확인하기 위해 적용된 이전 필터를 사용하여 테스트에서 익명 컨트롤러를 설치해야합니다.익명 컨트롤러에 대한 Rspec 스텁보기

describe ApplicationController do 
    controller do 
    before_filter :authenticated 

    def index  
    end 
    end 

    describe "user authenticated" do 
    let(:session_id){"session_id"} 
    let(:user){OpenStruct.new(:email => "[email protected]", :name => "Colin Gemmell")} 

    before do 
     request.cookies[:session_id] = session_id 
     UserSession.stub!(:find).with(session_id).and_return(user) 
     get :index 
    end 

    it { should assign_to(:user){user} } 

    end 
end 

을 그리고 응용 프로그램 컨트롤러는 다음과 같이이다 :

시험은 현재 다음과 같습니다

class ApplicationController < ActionController::Base 
    protect_from_forgery 

    def authenticated 
    @user = nil 
    end 
end 

내가 테스트를 실행 지금까지 나는 다음과 같은 오류

를 받고있을 때 내 문제입니다
1) ApplicationController user authenticated 
    Failure/Error: get :index 
    ActionView::MissingTemplate: 
    Missing template stub_resources/index with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml, :haml], :formats=>[:html], :locale=>[:en, :en]} in view paths "#<RSpec::Rails::ViewRendering::PathSetDelegatorResolver:0x984f310>" 

문서에 따르면보기는 rendered when running controller tests이 아니지만 아니오를 나타냅니다. 뷰가 존재하지 않기 때문에 이해할 수있는이 작업에 대한 스텁이 있음

누구나이 문제를 해결하거나 뷰를 스텁링하는 방법을 알 수 있습니다.

건배 콜린 G

답변

18

당신이 넣어이 피해 갈 수 없습니다 :

render :nothing => true 

#index 작업을 내부에?

+0

치료를 받았다. – pythonandchips

2

this blog post에서 변경하지 않는 한, RSpec 2는 컨트롤러 스펙이 작동하는보기 템플리트 파일이 필요합니다. 파일 자체는 렌더링되지 않으므로 (render_views을 추가하지 않으면) 내용이 중요하지 않습니다. 실제로는 touch index.html.erb과 함께 빈 파일을 추가 할 수 있습니다.

+0

"app/views/anonymous/index.html.erb"에 더미 템플릿을 추가해야했습니다. 더미 뷰를 spec/views 아래에 두는 방법을 알아 내지 못했습니다. – Rob

+0

당신이 맞습니다. 빈 템플릿은'spec/views'이 아니라'app/views'에 있습니다.이 템플릿은 레일스가 찾는 곳이기 때문에 의미가 있습니다. – zetetic

+0

application.rb에 다음 행을 추가하면 스펙/뷰 아래에 더미 뷰가 있는지 알 수 있습니다 :'# 익명 컨트롤러 테스트를위한 디렉토리 추가 'config.paths [ 'app/views'] < < "spec/views"인 경우 Rails.env.test?' – Rob

2

더 좋은 방법은 더미보기 디렉토리를 만드는 것입니다. 나는 그것이 실제로 유효한 뷰 테스트를 위해 spec/views를 사용하지 않을 것입니다. 대신,이 디렉토리 구조를 만들 : rspec2이뿐만 아니라 콘텐츠, 존재를 확인하기 때문에

spec/test_views/anonymous 
    index.html.erb 
    ... and any other anonymous controller templates you happen to need ... 

index.html.erb은 언급 한 바와 같이, 비어있을 수 있습니다.

는 그런 다음 application.rb 이니셜,이 선을 배치 :

# add a view directory for the anonymous controller tests 
config.paths['app/views'] << "spec/test_views" if Rails.env.test? 

참고 : 나는 몇 가지 이유가 작동하지 않습니다를 들어, test.rb에서 그 라인을 넣어했습니다.

관련 문제