Expect messages on double, but don't stub them

This commit is contained in:
Michael Miller 2019-11-16 10:59:13 -07:00
parent 186fa15a1a
commit 201fe614d1
3 changed files with 15 additions and 1 deletions

View file

@ -25,6 +25,7 @@ module Spectator::Expectations
end
def to(stub : Mocks::MethodStub) : Nil
Harness.current.mocks.expect(@actual.value, stub.name)
value = TestValue.new(stub.name, stub.to_s)
matcher = if (arguments = stub.arguments?)
Matchers::ReceiveArgumentsMatcher.new(value, arguments)

View file

@ -62,7 +62,10 @@ module Spectator::Mocks
{% if body && !body.is_a?(Nop) %}
{{body.body}}
{% else %}
unless ::Spectator::Harness.current.mocks.expected?(self, {{name.symbolize}})
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{name}}")
end
# This code shouldn't be reached, but makes the compiler happy to have a matching return type.
{% if definition.is_a?(TypeDeclaration) %}
%x = uninitialized {{definition.type}}
@ -75,6 +78,7 @@ module Spectator::Mocks
macro method_missing(call)
return self if @null
return self if ::Spectator::Harness.current.mocks.expected?(self, {{call.name.symbolize}})
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{call.name}}")
end

View file

@ -5,6 +5,7 @@ module Spectator::Mocks
private struct Entry
getter stubs = Deque(MethodStub).new
getter calls = Deque(MethodCall).new
getter expected = Set(Symbol).new
end
@all_instances = {} of String => Entry
@ -64,6 +65,14 @@ module Spectator::Mocks
fetch_type(type).calls.select { |call| call.name == method_name }
end
def expected?(object, method_name : Symbol) : Bool
fetch_instance(object).expected.includes?(method_name)
end
def expect(object, method_name : Symbol) : Nil
fetch_instance(object).expected.add(method_name)
end
private def fetch_instance(object)
key = unique_key(object)
if @entries.has_key?(key)