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, sets the log level to debug, and removes formatters to capture raw log entries for testing.
40 41 42 |
# File 'lib/lumberjack/capture_device/rspec.rb', line 40 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.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/lumberjack/capture_device/rspec.rb', line 60 def capture_logger_around_example(logger, example) capture_logger(logger, write_to_original: false) do |captured_device| example.run if example.exception logger.tag(rspec: {source_location: example.source_location, description: example.[:description]}) do captured_device. end 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.
23 24 25 |
# File 'lib/lumberjack/capture_device/rspec.rb', line 23 def include_log_entry(expected_hash) Lumberjack::CaptureDevice::IncludeLogEntryMatcher.new(expected_hash) end |