Class: RailsSemanticLogging::RSpec::LogSemanticMatcher
- Inherits:
-
Object
- Object
- RailsSemanticLogging::RSpec::LogSemanticMatcher
- Defined in:
- lib/rails_semantic_logging/rspec/matchers.rb
Overview
RSpec matcher for asserting log output from a block.
Usage:
expect { logger.info("hello") }.to log_semantic(level: :info, message: /hello/)
expect { logger.warn("oops", payload: { key: "val" }) }.to log_semantic(payload: { key: "val" })
Instance Method Summary collapse
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(expected) ⇒ LogSemanticMatcher
constructor
A new instance of LogSemanticMatcher.
- #matches?(block) ⇒ Boolean
- #supports_block_expectations? ⇒ Boolean
Constructor Details
#initialize(expected) ⇒ LogSemanticMatcher
Returns a new instance of LogSemanticMatcher.
72 73 74 75 76 77 78 |
# File 'lib/rails_semantic_logging/rspec/matchers.rb', line 72 def initialize(expected) @expected_level = expected[:level] @expected_message = expected[:message] @expected_named_tags = expected[:named_tags] @expected_payload = expected[:payload] @captured_logs = [] end |
Instance Method Details
#failure_message ⇒ Object
92 93 94 |
# File 'lib/rails_semantic_logging/rspec/matchers.rb', line 92 def "expected block to log a message matching #{expected_description}, but captured logs were:\n#{format_logs}" end |
#failure_message_when_negated ⇒ Object
96 97 98 |
# File 'lib/rails_semantic_logging/rspec/matchers.rb', line 96 def "expected block not to log a message matching #{expected_description}, but it did" end |
#matches?(block) ⇒ Boolean
80 81 82 83 84 85 86 |
# File 'lib/rails_semantic_logging/rspec/matchers.rb', line 80 def matches?(block) appender = InMemoryAppender.new CaptureAppender.with_capture_appender(appender) { block.call } @captured_logs = appender.logs @captured_logs.any? { |log| matches_log?(log) } end |
#supports_block_expectations? ⇒ Boolean
88 89 90 |
# File 'lib/rails_semantic_logging/rspec/matchers.rb', line 88 def supports_block_expectations? true end |