当我运行功能测试时,如何在Ruby on Rails应用程序中禁用救援处理程序?

时间:2021-02-20 03:52:59

I have a number of controllers in my Ruby on Rails apps with a rescue handler at the end of the action that basically catches any unhandled errors and returns some kind of "user friendly" error. However, when I'm doing rake test I'd like to have those default rescue handlers disabled so I can see the full error & stack trace. Is there any automated way to do this?

我的Ruby on Rails应用程序中有许多控制器,在操作结束时有一个救援处理程序,基本上可以捕获任何未处理的错误并返回某种“用户友好”错误。但是,当我进行rake测试时,我希望禁用那些默认的救援处理程序,这样我就可以看到完整的错误和堆栈跟踪。有没有自动化的方法来做到这一点?

Update to clarify: I have an action like this:

更新澄清:我有这样的行动:

def foo
  # do some stuff...
rescue
  render :text => "Exception: #{$!}" # this could be any kind of custom render
end

Now when I functional test this, if the exception is raised then I'm going to get just a little bit of info about the exception, but what I'd like is for it to act as though there's no rescue handler there, so I get the full debug information.

现在,当我对此进行功能测试时,如果异常被提出,那么我将获得关于异常的一些信息,但我想要的是它的行为好像那里没有救援处理程序,所以我获取完整的调试信息。

Update: SOLUTION

I did this:

我这样做了:

  rescue:
    raise unless Rails.env.production?
    render :text => "Exception: #{$!}" # this could be any kind of custom render
  end

5 个解决方案

#1


Not quite automated, but how modifying your code to re-throw exceptions whenever called within a test?

不是很自动化,但是如何修改代码以便在测试中调用时重新抛出异常?

Perhaps something like this:

也许是这样的:

def foo
  # do some stuff...
rescue
  raise if ENV["RAILS_ENV"] == "test"
  render :text => "Exception: #{$!}" # this could be any kind of custom render
end

#2


Have you looked at using the assert_raise( exception1, exception2, ... ) { block } call and then printing the exception from the block?

你有没有看过使用assert_raise(exception1,exception2,...){block}调用然后从块中打印异常?

#3


Which method are you using? There are two rescue methods in ActionController.

你使用哪种方法? ActionController中有两种救援方法。

I have this in my base controller:

我在我的基本控制器中有这个:

def rescue_action_in_public(exception)
    response_code = response_code_for_rescue(exception)
    status = interpret_status(response_code)
    respond_to do |format|
        format.html { render_optional_error_file response_code}
        format.js { render :update, :status => status  do |page| page.redirect_to(:url => error_page_url(status)) end}
end

end

This only displays custom errors in production mode.

这仅在生产模式下显示自定义错误。

#4


I think the easiest thing to do is verify that the correct render was called-- or whatever was different from the regular, non-exceptional case.

我认为最简单的方法是验证是否调用了正确的渲染 - 或者与常规的非常规情况有什么不同。

#5


You shouldn't need to disable your rescue block. Use the assert_raise method (as suggested by Scott), and in the block, call the method that you expect an exception from.

您不应该禁用救援块。使用assert_raise方法(如Scott所建议的),并在块中调用您期望异常的方法。

For example:

def test_throws_exception
  assert_raise Exception do
    raise_if_true(true)
  end
end

#1


Not quite automated, but how modifying your code to re-throw exceptions whenever called within a test?

不是很自动化,但是如何修改代码以便在测试中调用时重新抛出异常?

Perhaps something like this:

也许是这样的:

def foo
  # do some stuff...
rescue
  raise if ENV["RAILS_ENV"] == "test"
  render :text => "Exception: #{$!}" # this could be any kind of custom render
end

#2


Have you looked at using the assert_raise( exception1, exception2, ... ) { block } call and then printing the exception from the block?

你有没有看过使用assert_raise(exception1,exception2,...){block}调用然后从块中打印异常?

#3


Which method are you using? There are two rescue methods in ActionController.

你使用哪种方法? ActionController中有两种救援方法。

I have this in my base controller:

我在我的基本控制器中有这个:

def rescue_action_in_public(exception)
    response_code = response_code_for_rescue(exception)
    status = interpret_status(response_code)
    respond_to do |format|
        format.html { render_optional_error_file response_code}
        format.js { render :update, :status => status  do |page| page.redirect_to(:url => error_page_url(status)) end}
end

end

This only displays custom errors in production mode.

这仅在生产模式下显示自定义错误。

#4


I think the easiest thing to do is verify that the correct render was called-- or whatever was different from the regular, non-exceptional case.

我认为最简单的方法是验证是否调用了正确的渲染 - 或者与常规的非常规情况有什么不同。

#5


You shouldn't need to disable your rescue block. Use the assert_raise method (as suggested by Scott), and in the block, call the method that you expect an exception from.

您不应该禁用救援块。使用assert_raise方法(如Scott所建议的),并在块中调用您期望异常的方法。

For example:

def test_throws_exception
  assert_raise Exception do
    raise_if_true(true)
  end
end