From d1b54ad48fe73cdee8441d6287cb734068c5cae5 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sun, 3 Nov 2019 11:56:04 -0700 Subject: [PATCH] Better error for when stubs don't match return type --- src/spectator/mocks/double.cr | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/spectator/mocks/double.cr b/src/spectator/mocks/double.cr index 806e3fc..ccba68b 100644 --- a/src/spectator/mocks/double.cr +++ b/src/spectator/mocks/double.cr @@ -41,7 +41,11 @@ module Spectator::Mocks %call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args) @spectator_stub_calls << %call if (%stub = @spectator_stubs.find(&.callable?(%call))) - %stub.as(::Spectator::Mocks::GenericMethodStub(typeof(%method({{args.splat}})))).call(%args) + if (%cast = %stub.as?(::Spectator::Mocks::GenericMethodStub(typeof(%method({{args.splat}}))))) + %cast.call(%args) + else + raise "The return type of stub #{%stub} doesn't match the expected type #{typeof(%method({{args.splat}}))}" + end else %method({{args.splat}}) end @@ -52,7 +56,11 @@ module Spectator::Mocks %call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args) @spectator_stub_calls << %call if (%stub = @spectator_stubs.find(&.callable?(%call))) - %stub.as(::Spectator::Mocks::GenericMethodStub(typeof(%method({{args.splat}}) { |*%yield_args| yield *%yield_args }))).call(%args) + if (%cast = %stub.as?(::Spectator::Mocks::GenericMethodStub(typeof(%method({{args.splat}}) { |*%yield_args| yield *%yield_args })))) + %cast.call(%args) + else + raise "The return type of stub #{%stub} doesn't match the expected type #{typeof(%method({{args.splat}}) { |*%ya| yield *%ya })}" + end else %method({{args.splat}}) do |*%yield_args| yield *%yield_args