Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActionDispatch::IntegrationTest抑制异常_Ruby On Rails_Ruby_Ruby On Rails 5_Integration Testing_Actiondispatch - Fatal编程技术网

Ruby on rails ActionDispatch::IntegrationTest抑制异常

Ruby on rails ActionDispatch::IntegrationTest抑制异常,ruby-on-rails,ruby,ruby-on-rails-5,integration-testing,actiondispatch,Ruby On Rails,Ruby,Ruby On Rails 5,Integration Testing,Actiondispatch,调试失败的集成测试时,我一直遇到相同的问题,代码中引发的异常会被抑制,而不会显示在测试输出中 例如,对于以下控制器和测试: class RegistrationController::ApplicationController def创建 #引发异常的某些代码 结束 结束 class RegistrationFlowTest

调试失败的集成测试时,我一直遇到相同的问题,代码中引发的异常会被抑制,而不会显示在测试输出中

例如,对于以下控制器和测试:

class RegistrationController::ApplicationController
def创建
#引发异常的某些代码
结束
结束
class RegistrationFlowTest
输出类似于

Minitest::Assertion:预期响应为,但为
有没有办法查看引发的确切异常?而不仅仅是HTTP响应代码的差异

谢谢


Simon会发生这种情况,因为Rails控制器默认处理异常并提升500状态,使异常对测试套件不可见(如果在模型的单元测试中出现错误,这非常有用)。讨论了在测试套件中禁用此功能的选项或替代解决方案

该链接中的关键代码行,应添加到
test/integration/integration\u test\u helper.rb

ActionController::Base.class_eval do
  def perform_action
    perform_action_without_rescue
  end
end

Dispatcher.class_eval do
  def self.failsafe_response(output, status, exception = nil)
    raise exception
  end
end

编辑:我注意到这个链接现在已经很旧了。我对Rack非常熟悉,所以虽然第一个块看起来不错,但我不确定第二个块是否仍然是最新的。您可能需要查看它是否需要更新。

我建议解决此问题的方法是实际解析Rails提供的响应(至少在
测试
开发
环境中是默认的),其中包括错误的堆栈跟踪,并在测试失败的情况下进行处理。这样做的优点是,当出现不会导致测试失败的错误时(例如,您有意测试如何处理失败的场景),它不会输出stacktrace

我制作的这个小模块将允许您调用
assert\u response\u(带有\u错误)
来断言对调用的响应,但当响应不是您期望的响应时,以可读格式输出异常消息和堆栈跟踪

module ActionDispatch
  module Assertions
    module CustomResponseAssertions
      # Use this method when you want to assert a response body but also print the exception
      # and full stack trace to the test console.
      # Useful when you are getting errors in integration tests but don't know what they are.
      #
      # Example:
      # user_session.post create_gene_path, params: {...}
      # user_session.assert_response_with_errors :created
      def assert_response_with_errors(type, message = nil)
        assert_response(type, message)
      rescue Minitest::Assertion => e
        message = e.message
        message += "\nException message: #{@response.parsed_body[:exception]}"
        stack_trace = @response.parsed_body[:traces][:'Application Trace'].map { |line| line[:trace] }.join "\n"
        message += "\nException stack trace start"
        message += "\n#{stack_trace}"
        message += "\nException stack trace end"
        raise Minitest::Assertion, message
      end
    end
  end
end
要使用它,您需要在Rails将其堆栈加载到您的
test\u helper.rb
中之前将其包含在ActionDispatch::Assertions中。因此,只需在
test\u helper.rb
中预先添加include,如下所示:

ActionDispatch::Assertions.include ActionDispatch::Assertions::CustomResponseAssertions
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
...

可能只是你在你的参数上漏了一个冒号,所以不要用“
post sign\u-up\u path,params{username:'arnold',password:'123'}
,试试“
post sign\u-up\u-path,params:{username:'arnold',password:'123'}
,另外检查sign\u-in\u-up\u-path.Nope,只是写问题时的一个输入错误,但有个好消息:)我得到了
未捕获的异常:未初始化的常量调度程序
。那篇文章确实很老了。在rails指南中也找不到任何有用的内容。有一个名为
ActionDispatch::ShowExceptions
的中间件,但它已经包含在我的项目中。我还为测试环境进行了以下配置:
config.action\u dispatch.show\u exceptions=true
您是否尝试了有关控制器的第一个块?我现在尝试了,没有区别:(@rwold此问题的最大问题是,如果您正在测试的场景是一个错误HTTP状态(例如,测试上载无效格式的文件)而不是生成的错误HTTP状态(例如,身份验证失败),那么它将不起作用。如果您查看我的答案,它将允许您测试错误状态,并且在状态不是您期望的状态时仍然记录错误堆栈跟踪。