beforeフィルターとして使用されるアプリケーションコントローラーでメソッドをテストしようとしています。これを行うために、テストで匿名コントローラーをセットアップし、beforeフィルターを適用して正しく機能することを確認しました。
現在、テストは次のようになっています。
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 => "pythonandchips@gmail.com", :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>"
ドキュメントによると、コントローラーテストの実行時にビューはレンダリングされませんが、これはこのアクションにスタブが存在しないことを示しています(ビューが存在しないため理解できます)
誰もがこの問題を解決する方法やビューをスタブアウトする方法の手がかりを持っています。
乾杯コリンG