【问题标题】:Is there a way to not rescue in test environment in Ruby?有没有办法在 Ruby 的测试环境中不进行救援?
【发布时间】:2013-11-18 23:18:46
【问题描述】:
begin
  ...
rescue => e
  puts "Error: #{ e } at: \n#{ e.backtrace.first }"
end

这很好用,但有时会报告错误是在机架测试中。这使得很难弄清楚错误来自哪里。

所以,我想在测试环境中运行时禁用rescue子句。

这可能吗?有没有更好的方法?

【问题讨论】:

    标签: ruby exception rescue


    【解决方案1】:

    你可以这样做:

    rescue => e
      if Rails.env.test?
        raise
      else
        puts "Error: #{ e } at: \n#{ e.backtrace.first }"
      end
    end
    

    【讨论】:

    • 我同意。我认为救援首先不应该在那里。但这是完成您要求的唯一方法。
    • @BSeven 为什么你要在生产/开发中进行救援。我可能会建议一些其他方法。
    • 它是用于邮寄的。在生产中,如果邮件程序失败,我想写入日志。在测试环境中,如果失败了,我想要正常的错误信息。
    • 在 rails 4.2 我们可以在控制器中使用rescue_from ActiveRecord::RecordNotFound, with: :render_404(不确定早期版本)。
    猜你喜欢
    • 2011-09-07
    • 2013-07-13
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 2013-08-31
    相关资源
    最近更新 更多