diff --git a/src/spectator/mocks/type_registry.cr b/src/spectator/mocks/type_registry.cr index 5c4481e..0508c9f 100644 --- a/src/spectator/mocks/type_registry.cr +++ b/src/spectator/mocks/type_registry.cr @@ -15,5 +15,11 @@ module Spectator::Mocks end list << NilMethodStub.new(method_name, source, args) end + + def exists?(type_name : String, call : GenericMethodCall(T, NT)) : Bool forall T, NT + key = {type_name, call.name} + list = @@entries.fetch(key) { return false } + list.any?(&.callable?(call)) + end end end diff --git a/src/spectator/mocks/verifying_double.cr b/src/spectator/mocks/verifying_double.cr index 2c64b14..3f451cb 100644 --- a/src/spectator/mocks/verifying_double.cr +++ b/src/spectator/mocks/verifying_double.cr @@ -34,6 +34,11 @@ module Spectator::Mocks %args = ::Spectator::Mocks::GenericArguments.create({{args.splat}}) %call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args) ::Spectator::Harness.current.mocks.record_call(self, %call) + + unless ::Spectator::Mocks::TypeRegistry.exists?(T.to_s, %call) + raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{name}} - #{T} does not respond to #{%call}") + end + if (%stub = ::Spectator::Harness.current.mocks.find_stub(self, %call)) %stub.call!(%args) { %method({{args.splat}}) } else @@ -45,6 +50,11 @@ module Spectator::Mocks %args = ::Spectator::Mocks::GenericArguments.create({{args.splat}}) %call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args) ::Spectator::Harness.current.mocks.record_call(self, %call) + + unless ::Spectator::Mocks::TypeRegistry.exists?(T.to_s, %call) + raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{name}} - #{T} does not respond to #{%call}") + end + if (%stub = ::Spectator::Harness.current.mocks.find_stub(self, %call)) %stub.call!(%args) { %method({{args.splat}}) { |*%ya| yield *%ya } } else @@ -79,6 +89,10 @@ module Spectator::Mocks call = ::Spectator::Mocks::GenericMethodCall.new({{call.name.symbolize}}, args) ::Spectator::Harness.current.mocks.record_call(self, call) + unless ::Spectator::Mocks::TypeRegistry.exists?(T.to_s, call) + raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{call.name}} - #{T} does not respond to #{call}") + end + return self if @null return self if ::Spectator::Harness.current.mocks.expected?(self, call)