如何使config.exceptions_app与rspec一起工作

7ov*_*r21 5 rails-routing custom-exceptions rspec2 ruby-on-rails-3.2

在我的Rails 3.2应用程序中,我正在尝试使用config.exceptions_app通过路由表路由异常以呈现特定于错误的页面(尤其是401 Forbidden页面).这是我到目前为止配置的内容:

# 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
Run Code Online (Sandbox Code Playgroud)

现在问题的关键是:

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
Run Code Online (Sandbox Code Playgroud)

当我想渲染401响应时,我只是raise Error::Forbidden在开发环境中完美地工作.但是在rspec中运行示例时,例如:

it 'should return http forbidden' do
  put :update, :id => 12342343343
  response.should be_forbidden
end
Run Code Online (Sandbox Code Playgroud)

它悲惨地失败了:

1) UsersController PUT update when attempting to edit another record should return http forbidden
   Failure/Error: put :update, :id => 12342343343
   Error::Forbidden:
     Error::Forbidden
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我理解为什么这在我的测试环境中不起作用?我可以在ApplicationController中放一个#rescue_from,但如果我必须这样做才能让我的测试正常工作,我不知道config.exceptions_app首先使用的是什么.: - \

编辑:作为一种解决方法,我最后在config/environments/test.rb的结尾处添加了以下内容.这是hecka粗略,但似乎工作正常.

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
Run Code Online (Sandbox Code Playgroud)

sct*_*lsn 2

集合中config/environments/test.rb

config.action_dispatch.show_exceptions = true
Run Code Online (Sandbox Code Playgroud)