Method: ActiveSupport::Testing::ErrorReporterAssertions#assert_error_reported
- Defined in:
- activesupport/lib/active_support/testing/error_reporter_assertions.rb
#assert_error_reported(error_class = StandardError, &block) ⇒ Object
Assertion that the block should cause at least one exception to be reported to Rails.error.
Passes if the evaluated code in the yielded block reports a matching exception.
assert_error_reported(IOError) do
Rails.error.report(IOError.new("Oops"))
end
To test further details about the reported exception, you can use the return value.
report = assert_error_reported(IOError) do
# ...
end
assert_equal "Oops", report.error.
assert_equal "admin", report.context[:section]
assert_equal :warning, report.severity
assert_predicate report, :handled?
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# 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 = "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, ) end end |