This repository was archived by the owner on Nov 30, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -151,6 +151,16 @@ def deprecation(hash)
151
151
notify :deprecation , Notifications ::DeprecationNotification . from_hash ( hash )
152
152
end
153
153
154
+ # @private
155
+ # Provides a way to notify of an exception that is not tied to any
156
+ # particular exception (such as an exception encountered in a :suite hook).
157
+ # Exceptions will be formatted the same way they normally are.
158
+ def notify_non_example_exception ( exception , context_description )
159
+ example = Example . new ( AnonymousExampleGroup , context_description , { } )
160
+ presenter = Formatters ::ExceptionPresenter . new ( exception , example , :indentation => 0 )
161
+ message presenter . fully_formatted ( nil )
162
+ end
163
+
154
164
# @private
155
165
def finish
156
166
close_after do
Original file line number Diff line number Diff line change @@ -280,5 +280,27 @@ module RSpec::Core
280
280
reporter . finish
281
281
end
282
282
end
283
+
284
+ describe "#notify_non_example_exception" do
285
+ it "sends a `message` notification that contains the formatted exception details" do
286
+ formatter_out = StringIO . new
287
+ formatter = Formatters ::ProgressFormatter . new ( formatter_out )
288
+ reporter . register_listener formatter , :message
289
+
290
+ line = __LINE__ + 1
291
+ exception = 1 / 0 rescue $!
292
+ reporter . notify_non_example_exception ( exception , "NonExample Context" )
293
+
294
+ expect ( formatter_out . string ) . to start_with ( <<-EOS . gsub ( /^ +\| / , '' ) . chomp )
295
+ |
296
+ |NonExample Context
297
+ |Failure/Error: exception = 1 / 0 rescue $!
298
+ |
299
+ |ZeroDivisionError:
300
+ | divided by 0
301
+ |# #{ Metadata . relative_path ( __FILE__ ) } :#{ line }
302
+ EOS
303
+ end
304
+ end
283
305
end
284
306
end
You can’t perform that action at this time.
0 commit comments