class Adalog::MockLoggingAdapter::Base

Used as the superclass of all logging classes returned from ::new

Public Class Methods

new(**stub_method_overrides) click to toggle source

Allows for overriding stubbed methods which were initially built into the mock adapter. Does not explicitly restrict “overriding” to existing stubs, and so can be used to add additional stubs to a specific instance.

# File lib/adalog/mock_logging_adapter.rb, line 77
def initialize(**stub_method_overrides)
  stub_method_overrides.each do |message, value|
    define_singleton_method(message, &MockLoggingAdapter.stub_method(message, value))
  end
end
repo() click to toggle source
# File lib/adalog/mock_logging_adapter.rb, line 70
def repo        ; @repo         ; end
service_name() click to toggle source
# File lib/adalog/mock_logging_adapter.rb, line 69
def service_name; @service_name ; end

Public Instance Methods

_received_messages() click to toggle source

In case the service needs to stub-out a method named 'messages', we have this more direct, internal method name.

# File lib/adalog/mock_logging_adapter.rb, line 135
def _received_messages
  @_received_messages ||= []
end
messages() click to toggle source

Reader for the messages received by this mock, perhaps for use in testing details of particular calls.

# File lib/adalog/mock_logging_adapter.rb, line 91
def messages
  _received_messages
end
received?(message, exactly: :none, at_least: :none, at_most: :none) click to toggle source

For use in simple boolean asserts, if all you need to test is the number of times a message was received. Flexible keyword arguments, which the the following precedence and details:

  • If :exactly is specified, will return true only if the message count is exactly the value of :exactly.

  • When :exactly is specified, :at_least and :at_most are ignored.

  • If :at_least is specified, will return true only if the message count is equal to or greater than the value of :at_least.

  • If :at_most is specified, will return true only if the message count is equal to or less than the value of :at_most.

  • :at_least and :at_most can be used simultaneously, and will return true if the message count falls in the range (at_least..at_most), that is, inclusive.

  • If no keyword options are specified, the default behavior is equivalent to calling with the option `at_least: 1`.

Consequently, this method is as flexible as possible, to allow for expressive assertions in tests, such as:

received?('unsubscribe', at_least: 1)
received?('generate', exactly: 3)
received?('toggle', at_least: 2, at_most: 4)
# File lib/adalog/mock_logging_adapter.rb, line 117
def received?(message, exactly: :none, at_least: :none, at_most: :none)
  recv_count = _received_messages.select { |recv| recv.message == message }.count

  return recv_count == exactly unless :none == exactly
  # Substitute a default "at_least: 1" behavior for no options given.
  if :none == at_least and :none == at_most
    at_least = 1
  end

  result = true
  result = result && recv_count >= at_least unless :none == at_least
  result = result && recv_count <= at_most  unless :none == at_most
  result
end
repo() click to toggle source
# File lib/adalog/mock_logging_adapter.rb, line 86
def repo        ; self.class.repo         ; end
service_name() click to toggle source

Convenience instance method versions of class-level storage of service_name and repo.

# File lib/adalog/mock_logging_adapter.rb, line 85
def service_name; self.class.service_name ; end