命名空间
    
  
  
    
    方法
    
  
  
  
    
    
    
    
    
    
    
      实例公共方法
assert_error_reported(error_class = StandardError, &block) 链接
断言块会至少报告一个异常给 Rails.error。
如果所产生的代码块报告了一个匹配的异常,则断言通过。
assert_error_reported(IOError) do Rails.error.report(IOError.new("Oops")) end
要测试关于已报告异常的更多细节,您可以使用返回值。
report = assert_error_reported(IOError) do # ... end assert_equal "Oops", report.error.message assert_equal "admin", report.context[:section] assert_equal :warning, report.severity assert_predicate report, :handled?
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/testing/error_reporter_assertions.rb, line 88 def assert_error_reported(error_class = StandardError, &block) reports = ErrorCollector.record do _assert_nothing_raised_or_warn("assert_error_reported", &block) end if reports.empty? assert(false, "Expected a #{error_class.name} to be reported, but there were no errors reported.") elsif (report = reports.find { |r| error_class === r.error }) self.assertions += 1 report else message = "Expected a #{error_class.name} to be reported, but none of the " \ "#{reports.size} reported errors matched: \n" \ "#{reports.map { |r| r.error.class.name }.join("\n ")}" assert(false, message) end end
assert_no_error_reported(&block) 链接
断言块不应向 Rails.error 报告异常。
如果所产生的代码块未报告任何异常,则断言通过。
assert_no_error_reported do perform_service(param: 'no_exception') end
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/testing/error_reporter_assertions.rb, line 62 def assert_no_error_reported(&block) reports = ErrorCollector.record do _assert_nothing_raised_or_warn("assert_no_error_reported", &block) end assert_predicate(reports, :empty?) end
capture_error_reports(error_class = StandardError, &block) 链接
捕获块内与给定错误类匹配的报告错误。
reports = capture_error_reports(IOError) do Rails.error.report(IOError.new("Oops")) Rails.error.report(IOError.new("Oh no")) Rails.error.report(StandardError.new) end assert_equal 2, reports.size assert_equal "Oops", reports.first.error.message assert_equal "Oh no", reports.last.error.message
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/testing/error_reporter_assertions.rb, line 118 def capture_error_reports(error_class = StandardError, &block) reports = ErrorCollector.record(&block) reports.select { |r| error_class === r.error } end