Class: Adalog::MockLoggingAdapter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/adalog/mock_logging_adapter.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**stub_method_overrides) ⇒ Base

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.



77
78
79
80
81
# 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

Class Method Details

.repoObject



70
# File 'lib/adalog/mock_logging_adapter.rb', line 70

def repo        ; @repo         ; end

.service_nameObject



69
# File 'lib/adalog/mock_logging_adapter.rb', line 69

def service_name; @service_name ; end

Instance Method Details

#_received_messagesObject

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



135
136
137
# File 'lib/adalog/mock_logging_adapter.rb', line 135

def _received_messages
  @_received_messages ||= []
end

#messagesObject

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



91
92
93
# File 'lib/adalog/mock_logging_adapter.rb', line 91

def messages
  _received_messages
end

#received?(message, exactly: :none, at_least: :none, at_most: :none) ⇒ Boolean

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)

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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

#repoObject



86
# File 'lib/adalog/mock_logging_adapter.rb', line 86

def repo        ; self.class.repo         ; end

#service_nameObject

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



85
# File 'lib/adalog/mock_logging_adapter.rb', line 85

def service_name; self.class.service_name ; end