2010-11-04 104 views
0

在我們的Rails應用程序2.3,我已經設置了路由錯誤一rescue_from如下:如何測試rescue_from的路由失敗?

rescue_from ActionController::RoutingError,  :with => :redirect_or_render_error 
rescue_from ActionController::UnknownController, :with => :redirect_or_render_error 
rescue_from ActionController::UnknownAction,  :with => :redirect_or_render_error 

,然後在我的redirect_or_render_error方法我重定向某些URL(從DB拉,所以我不能只是使用routes.rb),我想測試這個。我在做這在我的索引頁的功能測試(是正確的地方?)像這樣:

@request.remote_addr = '12.34.56.78' # fake remote request 
get '/example' 
assert_redirected_to '/example_things/123456' 

我得到

ActionController::RoutingError: No route matches {:action=>"/example", :controller=>"home"} 

即使它在發展。如何測試rescue_from的路由失敗?

回答

0

原來我不得不使用一個集成測試,而不是一個功能測試,就像這樣:

get '/example', {}, :remote_addr => '12.34.56.78' 
assert_redirected_to '/example_things/123456' 

,我也需要設置config.action_controller.consider_all_requests_local在配置錯誤/environments/test.rb

+0

使用集成測試解決了此處所述的問題。但是,我在測試環境文件中將'config.action_controller.consider_all_requests_local'設置爲'true'。 – berto 2011-04-12 00:54:18

0

你斷言你的路線是真實的。事實並非如此。你有一組你不打算映射的路線,你將在異常處理程序中處理它。 (另外:我認爲你應該儘可能在你的routes.rb中處理儘可能多的情況,並讓一個普通的Ruby方法來處理任何進一步的操作。)

無論如何,要真正測試這個,你想要做線沿線的更多:

@request.remote_addr = '12.34.56.78' # fake remote request 
assert_raise ActionController::RoutingError do 
    get '/example' 
end 
+0

那不測試異常處理程序。我想測試重定向的工作原理。 – Simon 2010-11-05 07:27:51

相關問題