From 5a072301af1626ff9d22d977b605c97c628bb116 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 9 Nov 2019 09:15:41 -0700 Subject: [PATCH] Don't require registration --- src/spectator/mocks/registry.cr | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/spectator/mocks/registry.cr b/src/spectator/mocks/registry.cr index f3504f5..3672d30 100644 --- a/src/spectator/mocks/registry.cr +++ b/src/spectator/mocks/registry.cr @@ -15,30 +15,25 @@ module Spectator::Mocks @entries.clear end - def register(object) : Nil - key = unique_key(object) - @@entries[key] = Entry.new - end - def add_stub(object, stub : MethodStub) : Nil - key = unique_key(object) - @@entries[key].stubs << stub - rescue KeyError - raise "Cannot stub unregistered mock" + fetch(object).stubs << stub end def find_stub(object, call : GenericMethodCall(T, NT)) forall T, NT - key = unique_key(object) - @@entries[key].stubs.find(&.callable?(call)) - rescue KeyError - raise "Cannot stub unregistered mock" + fetch(object).stubs.find(&.callable?(call)) end def record_call(object, call : MethodCall) : Nil + fetch(object).calls << call + end + + private def fetch(object) key = unique_key(object) - @@entries[key].calls << call - rescue KeyError - raise "Cannot record call for unregistered mock" + if @@entries.has_key?(key) + @@entries[key] + else + @@entries[key] = Entry.new + end end private def unique_key(reference : Reference)