Add methods for recording calls to stubs

This commit is contained in:
Michael Miller 2022-06-08 08:18:31 -06:00
parent c70e4792af
commit c98edcec5d
No known key found for this signature in database
GPG key ID: AC78B32D30CE34A2
4 changed files with 39 additions and 1 deletions

View file

@ -63,6 +63,8 @@ module Spectator
end
end
@calls = [] of MethodCall
# Creates the double.
#
# An initial set of *stubs* can be provided.
@ -111,6 +113,14 @@ module Spectator
@stubs.any? { |stub| stub.method == method }
end
def _spectator_record_call(call : MethodCall) : Nil
@calls << call
end
def _spectator_calls(method : Symbol) : Enumerable(MethodCall)
@calls.select { |call| call.method == method }
end
# Returns the double's name formatted for user output.
private def _spectator_stubbed_name : String
{% if anno = @type.annotation(StubbedName) %}

View file

@ -52,6 +52,8 @@ module Spectator
@_spectator_stubs = nil
end
private getter _spectator_calls = [] of ::Spectator::MethodCall
# Returns the mock's name formatted for user output.
private def _spectator_stubbed_name : String
\{% if anno = @type.annotation(::Spectator::StubbedName) %}
@ -126,6 +128,10 @@ module Spectator
@@_spectator_mock_registry.delete(self)
end
def _spectator_calls
[] of ::Spectator::MethodCall
end
# Returns the mock's name formatted for user output.
private def _spectator_stubbed_name : String
\{% if anno = @type.annotation(::Spectator::StubbedName) %}

View file

@ -8,15 +8,23 @@ module Spectator
#
# Bridges functionality between mocks and stubs
# Implements the abstracts methods from `Stubbable`.
#
# Types including this module will need to implement `#_spectator_stubs`.
# It should return a mutable list of stubs.
# This is used to store the stubs for the mocked type.
#
# Additionally, the `#_spectator_calls` (getter with no arguments) must be implemented.
# It should return a mutable list of method calls.
# This is used to store the calls to stubs for the mocked type.
module Mocked
include Stubbable
# Retrieves an enumerable collection of stubs.
# Retrieves an mutable collection of stubs.
abstract def _spectator_stubs
# Retrieves an mutable collection of calls to stubs.
abstract def _spectator_calls
def _spectator_define_stub(stub : ::Spectator::Stub) : Nil
_spectator_stubs.unshift(stub)
end
@ -33,6 +41,14 @@ module Spectator
_spectator_stubs.any? { |stub| stub.method == method }
end
def _spectator_record_call(call : MethodCall) : Nil
_spectator_calls << call
end
def _spectator_calls(method : Symbol) : Enumerable(MethodCall)
_spectator_calls.select { |call| call.method == method }
end
# Method called when a stub isn't found.
#
# The received message is captured in *call*.

View file

@ -31,6 +31,12 @@ module Spectator
# Clears all previously defined stubs.
abstract def _spectator_clear_stubs : Nil
# Saves a call that was made to a stubbed method.
abstract def _spectator_record_call(call : MethodCall) : Nil
# Retrieves all previously saved calls for the specified method.
abstract def _spectator_calls(method : Symbol) : Enumerable(MethodCall)
# Method called when a stub isn't found.
#
# The received message is captured in *call*.