我有一个从控制器中调用的模块方法,如下所示:
class MyController
include MyControllerExtension
end
module MyControllerExtension
def some_method
@variable.update!(params)
respond_to do |format|
format.html { redirect_to action: 'show' }
end
rescue App::CustomErrors::SomeMethodError => e
errors_catch_method(e)
end
def errors_catch_method(error)
case error
when App::CustomErrors::SomeMethodError
formatted_redirect_method 'index'
when App::CustomErrors::AnotherMethodError
render action: 'maintenance'
end
def formatted_redirect_method(url)
format.html { redirect_to action: url }
end
end
现在,每当引发错误并被redirected path
块捕获时,我就需要测试rescue
。测试代码如下:
MyController::any_instance.stubs(:some_method).raises(App::CustomErrors::SomeMethodError)
每当测试代码到达some_method
时,都会引发错误,但不会将其重定向到formatted_redirect_method
中的URL。实际上甚至没有errors_catch_method
被调用。它仅被引发而不进入rescue
块。我应该怎么做才能使minitest
在rescue
块中声明代码?