Better error for when stubs don't match return type

This commit is contained in:
Michael Miller 2019-11-03 11:56:04 -07:00
parent e6c1a6b2d2
commit d1b54ad48f

View file

@ -41,7 +41,11 @@ module Spectator::Mocks
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args) %call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
@spectator_stub_calls << %call @spectator_stub_calls << %call
if (%stub = @spectator_stubs.find(&.callable?(%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 else
%method({{args.splat}}) %method({{args.splat}})
end end
@ -52,7 +56,11 @@ module Spectator::Mocks
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args) %call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
@spectator_stub_calls << %call @spectator_stub_calls << %call
if (%stub = @spectator_stubs.find(&.callable?(%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 else
%method({{args.splat}}) do |*%yield_args| %method({{args.splat}}) do |*%yield_args|
yield *%yield_args yield *%yield_args