Allow stubbing/overriding anonymous double methods

This commit is contained in:
Michael Miller 2019-11-14 17:12:27 -07:00
parent b9fe1b6a60
commit e44f0229d6
2 changed files with 17 additions and 3 deletions

View file

@ -8,8 +8,15 @@ module Spectator::Mocks
end
macro method_missing(call)
@values.fetch({{call.name.symbolize}}) do
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{call.name}}")
args = ::Spectator::Mocks::GenericArguments.create({{call.args.splat}})
call = ::Spectator::Mocks::GenericMethodCall.new({{call.name.symbolize}}, args)
::Spectator::Harness.current.mocks.record_call(self, call)
if (stub = ::Spectator::Harness.current.mocks.find_stub(self, call))
stub.call!(args, typeof(@values.fetch({{call.name.symbolize}}) { raise }))
else
@values.fetch({{call.name.symbolize}}) do
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{call.name}}")
end
end
end
end

View file

@ -4,7 +4,14 @@ module Spectator::Mocks
end
macro method_missing(call)
@values.fetch({{call.name.symbolize}}) { self }
args = ::Spectator::Mocks::GenericArguments.create({{call.args.splat}})
call = ::Spectator::Mocks::GenericMethodCall.new({{call.name.symbolize}}, args)
::Spectator::Harness.current.mocks.record_call(self, call)
if (stub = ::Spectator::Harness.current.mocks.find_stub(self, call))
stub.call!(args, typeof(@values.fetch({{call.name.symbolize}}) { self }))
else
@values.fetch({{call.name.symbolize}}) { self }
end
end
end
end