Skip to content

Commit 76efa0f

Browse files
authored
Merge pull request rails#54809 from Shopify/times-assert-error-reported
Introduce `capture_error_reports`
2 parents 8090d41 + 22dd98a commit 76efa0f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

activesupport/CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
* Introduce ActiveSupport::Testing::ErrorReporterAssertions#capture_error_reports
2+
3+
Captures all reported errors from within the block that match the given
4+
error class.
5+
6+
```ruby
7+
reports = capture_error_reports(IOError) do
8+
Rails.error.report(IOError.new("Oops"))
9+
Rails.error.report(IOError.new("Oh no"))
10+
Rails.error.report(StandardError.new)
11+
end
12+
13+
assert_equal 2, reports.size
14+
assert_equal "Oops", reports.first.error.message
15+
assert_equal "Oh no", reports.last.error.message
16+
```
17+
18+
*Andrew Novoselac*
19+
120
* Introduce ActiveSupport::ErrorReporter#add_middleware
221

322
When reporting an error, the error context middleware will be called with the reported error

activesupport/lib/active_support/testing/error_reporter_assertions.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ def assert_error_reported(error_class = StandardError, &block)
102102
assert(false, message)
103103
end
104104
end
105+
106+
# Captures reported errors from within the block that match the given
107+
# error class.
108+
#
109+
# reports = capture_error_reports(IOError) do
110+
# Rails.error.report(IOError.new("Oops"))
111+
# Rails.error.report(IOError.new("Oh no"))
112+
# Rails.error.report(StandardError.new)
113+
# end
114+
#
115+
# assert_equal 2, reports.size
116+
# assert_equal "Oops", reports.first.error.message
117+
# assert_equal "Oh no", reports.last.error.message
118+
def capture_error_reports(error_class = StandardError, &block)
119+
reports = ErrorCollector.record(&block)
120+
reports.select { |r| error_class === r.error }
121+
end
105122
end
106123
end
107124
end

0 commit comments

Comments
 (0)