1

Rails 3 は、rescue_from ハンドラーを無視しているように見えるため、以下のリダイレクトをテストできません。

class ApplicationController < ActionController::Base

  rescue_from ActionController::RoutingError, :with => :rescue_404 

  def rescue_404
    flash[:notice] = "Error 404. The url <i>'#{env["vidibus-routing_error.request_uri"]}'</i> does not exist on this website."
    redirect_to root_path
  end
end

機能テストと統合テストの両方で、この rescue_from は無視され、エラーが発生します。

ActionController::RoutingError: No route matches "/non_existent_url"
    test/integration/custom_404_test.rb:5:in `test_404'

これがテストで適切に「キャッチ」されていることを確認するにはどうすればよいですか?

4

1 に答える 1

2

Rails 3ActionController::RoutingErrorはミドルウェアで処理するためApplicationController::rescue_from、例外は表示されません。Rails コア チームは、修正を決定するまでroutes.rb( GitHub の問題)の下部にあるキャッチオール ルートを使用することをお勧めします。

1 つのオプションは、キャッチオール ルートを使用してルーティング エラーを処理し、ヒットする例外を手動で発生させることですrescue_from(この問題に関するブログ投稿のコード)。

# routes.rb
match "*path", :to => "application#routing_error"

# application_controller.rb
rescue_from ActionController::RoutingError, :with => :render_not_found

def routing_error
  raise ActionController::RoutingError.new(params[:path])
end

def render_not_found
  render :template => "misc/404"
end
于 2012-04-28T11:19:08.980 に答える