Class: Lumberjack::CaptureDevice::IncludeLogEntryMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/lumberjack/capture_device/include_log_entry_matcher.rb

Overview

RSpec matcher for checking captured logs for specific entries.

Instance Method Summary collapse

Constructor Details

#initialize(expected_hash) ⇒ IncludeLogEntryMatcher

Initialize the matcher with expected log entry attributes.

Parameters:

  • expected_hash (Hash)

    Expected log entry attributes to match against.



8
9
10
11
# File 'lib/lumberjack/capture_device/include_log_entry_matcher.rb', line 8

def initialize(expected_hash)
  @expected_hash = expected_hash.transform_keys(&:to_sym)
  @logger = nil
end

Instance Method Details

#descriptionString

Provide a description of what this matcher checks.

Returns:

  • (String)

    A human-readable description of the matcher.



51
52
53
# File 'lib/lumberjack/capture_device/include_log_entry_matcher.rb', line 51

def description
  "have logged entry with #{expectation_description(@expected_hash)}"
end

#failure_messageString

Generate a failure message when the matcher fails.

Returns:

  • (String)

    A formatted failure message.



29
30
31
32
33
34
35
# File 'lib/lumberjack/capture_device/include_log_entry_matcher.rb', line 29

def failure_message
  if valid_logger?
    formatted_failure_message(@logger, @expected_hash)
  else
    wrong_object_type_message(@logger)
  end
end

#failure_message_when_negatedString

Generate a failure message when the negated matcher fails.

Returns:

  • (String)

    A formatted failure message for negated expectations.



40
41
42
43
44
45
46
# File 'lib/lumberjack/capture_device/include_log_entry_matcher.rb', line 40

def failure_message_when_negated
  if valid_logger?
    formatted_negated_failure_message(@logger, @expected_hash)
  else
    wrong_object_type_message(@logger)
  end
end

#matches?(actual) ⇒ Boolean

Check if the logger contains a log entry matching the expected attributes.

Parameters:

  • actual (Lumberjack::Logger, Lumberjack::ForkedLogger)

    The logger to check. The logger must be using a Lumberjack::Device::Test device.

Returns:

  • (Boolean)

    True if a matching log entry is found.



18
19
20
21
22
23
24
# File 'lib/lumberjack/capture_device/include_log_entry_matcher.rb', line 18

def matches?(actual)
  @logger = actual
  return false unless valid_logger?

  device = @logger.is_a?(Lumberjack::Device::Test) ? @logger : @logger.device
  device.include?(@expected_hash)
end