Class: RailsSemanticLogging::RSpec::LogSemanticMatcher

Inherits:
Object
  • Object
show all
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

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_messageObject



92
93
94
# File 'lib/rails_semantic_logging/rspec/matchers.rb', line 92

def failure_message
  "expected block to log a message matching #{expected_description}, but captured logs were:\n#{format_logs}"
end

#failure_message_when_negatedObject



96
97
98
# File 'lib/rails_semantic_logging/rspec/matchers.rb', line 96

def failure_message_when_negated
  "expected block not to log a message matching #{expected_description}, but it did"
end

#matches?(block) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


88
89
90
# File 'lib/rails_semantic_logging/rspec/matchers.rb', line 88

def supports_block_expectations?
  true
end