Implement fallback for methods without a stub

This commit is contained in:
Michael Miller 2022-04-15 17:27:30 -06:00
parent 6f04e0b9da
commit 23d2c014b4
No known key found for this signature in database
GPG Key ID: 32B47AE8F388A1FF
1 changed files with 17 additions and 6 deletions

View File

@ -13,8 +13,7 @@ module Spectator
# This method can also raise an error if it's impossible to return something. # This method can also raise an error if it's impossible to return something.
def _spectator_stub_fallback(call : MethodCall, &) def _spectator_stub_fallback(call : MethodCall, &)
if _spectator_stub_for_method?(call.method) if _spectator_stub_for_method?(call.method)
# FIXME: Don't log to top-level Spectator logger (use mock or double logger). Spectator::Log.info do # FIXME: Don't log to top-level Spectator logger (use mock or double logger).
Spectator::Log.info do
"Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)." "Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)."
end end
@ -31,8 +30,14 @@ module Spectator
# Yield to call the original method's implementation. # Yield to call the original method's implementation.
# The stubbed method returns the value returned by this method. # The stubbed method returns the value returned by this method.
# This method can also raise an error if it's impossible to return something. # This method can also raise an error if it's impossible to return something.
def _spectator_stub_fallback(call : MethodCall, _type, &) def _spectator_stub_fallback(call : MethodCall, type, &)
_spectator_stub_fallback(call) { yield } value = _spectator_stub_fallback(call) { yield }
begin
type.cast(value)
rescue TypeCastError
raise TypeCastError.new("#{_spectator_stubbed_name} received message #{call} and is attempting to return `#{value.inspect}`, but returned type must be `#{type}`.")
end
end end
# Method called when a stub isn't found. # Method called when a stub isn't found.
@ -60,8 +65,14 @@ module Spectator
# The expected return type is provided by *type*. # The expected return type is provided by *type*.
# The stubbed method returns the value returned by this method. # The stubbed method returns the value returned by this method.
# This method can also raise an error if it's impossible to return something. # This method can also raise an error if it's impossible to return something.
def _spectator_abstract_stub_fallback(call : MethodCall, _type) def _spectator_abstract_stub_fallback(call : MethodCall, type)
_spectator_abstract_stub_fallback(call) value = _spectator_abstract_stub_fallback(call)
begin
type.cast(value)
rescue TypeCastError
raise TypeCastError.new("#{_spectator_stubbed_name} received message #{call} and is attempting to return `#{value.inspect}`, but returned type must be `#{type}`.")
end
end end
end end
end end