Module: Lumberjack::CaptureDevice::RSpec
- Defined in:
- lib/lumberjack/capture_device/rspec.rb
Overview
RSpec helper methods for working with CaptureDevice.
Instance Method Summary collapse
-
#capture_logger(logger, write_to_original: true) {|device| ... } ⇒ Lumberjack::CaptureDevice
Capture log entries from a logger within a block.
-
#capture_logger_around_example(logger, example) ⇒ Object
RSpec around hook to automatically capture logs for each example.
-
#include_log_entry(expected_hash) ⇒ Lumberjack::CaptureDevice::IncludeLogEntryMatcher
Create a matcher for checking if a log entry is included in the captured logs.
Instance Method Details
#capture_logger(logger, write_to_original: true) {|device| ... } ⇒ Lumberjack::CaptureDevice
Capture log entries from a logger within a block. This method temporarily replaces the logger's device with a CaptureDevice and sets the log level to debug. The logger's formatters remain active, so captured entries contain the same formatted values that would have been logged.
39 40 41 |
# File 'lib/lumberjack/capture_device/rspec.rb', line 39 def capture_logger(logger, write_to_original: true, &block) Lumberjack::CaptureDevice.capture(logger, write_to_original: write_to_original, &block) end |
#capture_logger_around_example(logger, example) ⇒ Object
RSpec around hook to automatically capture logs for each example. The captured logs are only written to the original logger if the example fails. This helps keep the logs more usable for debugging test failures since it removes all the noise from passing tests.
This is designed for CI environments where you can save the logs as artifacts of the test run.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/lumberjack/capture_device/rspec.rb', line 59 def capture_logger_around_example(logger, example) capture_logger(logger, write_to_original: false) do |captured_device| example.run if example.exception rspec_attributes = {rspec: {location: example.location, description: example.[:description]}} captured_device.(attributes: rspec_attributes) end end end |
#include_log_entry(expected_hash) ⇒ Lumberjack::CaptureDevice::IncludeLogEntryMatcher
Create a matcher for checking if a log entry is included in the captured logs. This matcher provides better error messages than using the include? method directly.
21 22 23 |
# File 'lib/lumberjack/capture_device/rspec.rb', line 21 def include_log_entry(expected_hash) Lumberjack::CaptureDevice::IncludeLogEntryMatcher.new(expected_hash) end |