From 989086c4fcd27c3f283f50b0ee836d645233ac3c Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 4 Jul 2022 15:35:12 -0600 Subject: [PATCH] Add method to clear recorded calls --- src/spectator/mocks/double.cr | 4 ++++ src/spectator/mocks/mocked.cr | 4 ++++ src/spectator/mocks/stubbable.cr | 5 +++++ src/spectator/mocks/stubbed_type.cr | 4 ++-- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/spectator/mocks/double.cr b/src/spectator/mocks/double.cr index 022d782..f03e5f2 100644 --- a/src/spectator/mocks/double.cr +++ b/src/spectator/mocks/double.cr @@ -121,6 +121,10 @@ module Spectator @calls end + def _spectator_clear_calls : Nil + @calls.clear + end + # Returns the double's name formatted for user output. private def _spectator_stubbed_name : String {% if anno = @type.annotation(StubbedName) %} diff --git a/src/spectator/mocks/mocked.cr b/src/spectator/mocks/mocked.cr index 5ed47ad..c2f9b38 100644 --- a/src/spectator/mocks/mocked.cr +++ b/src/spectator/mocks/mocked.cr @@ -46,6 +46,10 @@ module Spectator _spectator_calls.select { |call| call.method == method } end + def _spectator_clear_calls : Nil + _spectator_calls.clear + end + # Method called when a stub isn't found. # # The received message is captured in *call*. diff --git a/src/spectator/mocks/stubbable.cr b/src/spectator/mocks/stubbable.cr index 2dca87f..5f6a473 100644 --- a/src/spectator/mocks/stubbable.cr +++ b/src/spectator/mocks/stubbable.cr @@ -38,6 +38,9 @@ module Spectator # Retrieves all previously saved calls. abstract def _spectator_calls + # Clears all previously saved calls. + abstract def _spectator_clear_calls : Nil + # Method called when a stub isn't found. # # The received message is captured in *call*. @@ -387,6 +390,8 @@ module Spectator extend StubbedType private class_getter _spectator_stubs : Array(::Spectator::Stub) = [] of ::Spectator::Stub + + class_getter _spectator_calls : Array(::Spectator::MethodCall) = [] of ::Spectator::MethodCall end end end diff --git a/src/spectator/mocks/stubbed_type.cr b/src/spectator/mocks/stubbed_type.cr index 40b2fc1..1be4745 100644 --- a/src/spectator/mocks/stubbed_type.cr +++ b/src/spectator/mocks/stubbed_type.cr @@ -28,8 +28,8 @@ module Spectator _spectator_calls << call end - def _spectator_calls - [] of MethodCall + def _spectator_clear_calls : Nil + _spectator_calls.clear end def _spectator_stub_fallback(call : MethodCall, &)