私のRails 3.2アプリケーションでは、config.exceptions_appを使用してエラーテーブル(特に禁止されている401ページ)をレンダリングするためにルーティングテーブルを通して例外をルーティングしようとしています。ここで私は設定のために、これまで持っているものです:rspecでconfig.exceptions_appを動作させるにはどうすればいいですか
# application.rb
config.action_dispatch.rescue_responses.merge!('Error::Forbidden' => :forbidden)
config.exceptions_app = ->(env) { ErrorsController.action(:show).call(env) }
# development.rb
config.consider_all_requests_local = false
# test.rb
config.consider_all_requests_local = false
そして今、問題の肉:私はレンダリングする
module Error
class Forbidden < StandardError
end
end
class ErrorsController < ApplicationController
layout 'error'
def show
exception = env['action_dispatch.exception']
status_code = ActionDispatch::ExceptionWrapper.new(env, exception).status_code
rescue_response = ActionDispatch::ExceptionWrapper.rescue_responses[exception.class.name]
render :action => rescue_response, :status => status_code, :formats => [:html]
end
def forbidden
render :status => :forbidden, :formats => [:html]
end
end
その401応答、私だけでraise Error::Forbidden
開発でいます、環境は完全に機能します。 RSpecの例を実行している場合でも、例えば:
it 'should return http forbidden' do
put :update, :id => 12342343343
response.should be_forbidden
end
それは無残に失敗します。
1) UsersController PUT update when attempting to edit another record should return http forbidden
Failure/Error: put :update, :id => 12342343343
Error::Forbidden:
Error::Forbidden
誰かが、これは私のテスト環境では動作しませんなぜ私が理解するのに役立つだろうか? I ApplicationControllerに#rescue_fromを置くことができますが、私がテストを行うためにそれをしなければならない場合、最初にconfig.exceptions_app
の使用のポイントがわかりません。 : - \
EDIT:回避策として、私は設定/環境/ test.rbの末尾に次を入れて巻き上げそれは総heckaだが、大丈夫動作しているようです。 config/environments/test.rb
で
module Error
def self.included(base)
_not_found = -> do
render :status => :not_found, :text => 'not found'
end
_forbidden = -> do
render :status => :forbidden, :text => 'forbidden'
end
base.class_eval do
rescue_from 'ActiveRecord::RecordNotFound', :with => _not_found
rescue_from 'ActionController::UnknownController', :with => _not_found
rescue_from 'AbstractController::ActionNotFound', :with => _not_found
rescue_from 'ActionController::RoutingError', :with => _not_found
rescue_from 'Error::Forbidden', :with => _forbidden
end
end
end
お返事ありがとうございますが、私はすでにそのファイルにその行があり、問題は解決していません。私が上に示したエラーモジュールをコメントアウトすると、テストは失敗に戻ります。 – 7over21