跳至內容 跳至搜尋
命名空間
方法
A

實例公開方法

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?
# 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
# 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