mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Expect messages on double, but don't stub them
This commit is contained in:
parent
186fa15a1a
commit
201fe614d1
3 changed files with 15 additions and 1 deletions
|
@ -25,6 +25,7 @@ module Spectator::Expectations
|
||||||
end
|
end
|
||||||
|
|
||||||
def to(stub : Mocks::MethodStub) : Nil
|
def to(stub : Mocks::MethodStub) : Nil
|
||||||
|
Harness.current.mocks.expect(@actual.value, stub.name)
|
||||||
value = TestValue.new(stub.name, stub.to_s)
|
value = TestValue.new(stub.name, stub.to_s)
|
||||||
matcher = if (arguments = stub.arguments?)
|
matcher = if (arguments = stub.arguments?)
|
||||||
Matchers::ReceiveArgumentsMatcher.new(value, arguments)
|
Matchers::ReceiveArgumentsMatcher.new(value, arguments)
|
||||||
|
|
|
@ -62,7 +62,10 @@ module Spectator::Mocks
|
||||||
{% if body && !body.is_a?(Nop) %}
|
{% if body && !body.is_a?(Nop) %}
|
||||||
{{body.body}}
|
{{body.body}}
|
||||||
{% else %}
|
{% else %}
|
||||||
|
unless ::Spectator::Harness.current.mocks.expected?(self, {{name.symbolize}})
|
||||||
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{name}}")
|
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.
|
# This code shouldn't be reached, but makes the compiler happy to have a matching return type.
|
||||||
{% if definition.is_a?(TypeDeclaration) %}
|
{% if definition.is_a?(TypeDeclaration) %}
|
||||||
%x = uninitialized {{definition.type}}
|
%x = uninitialized {{definition.type}}
|
||||||
|
@ -75,6 +78,7 @@ module Spectator::Mocks
|
||||||
|
|
||||||
macro method_missing(call)
|
macro method_missing(call)
|
||||||
return self if @null
|
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}}")
|
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{call.name}}")
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,6 +5,7 @@ module Spectator::Mocks
|
||||||
private struct Entry
|
private struct Entry
|
||||||
getter stubs = Deque(MethodStub).new
|
getter stubs = Deque(MethodStub).new
|
||||||
getter calls = Deque(MethodCall).new
|
getter calls = Deque(MethodCall).new
|
||||||
|
getter expected = Set(Symbol).new
|
||||||
end
|
end
|
||||||
|
|
||||||
@all_instances = {} of String => Entry
|
@all_instances = {} of String => Entry
|
||||||
|
@ -64,6 +65,14 @@ module Spectator::Mocks
|
||||||
fetch_type(type).calls.select { |call| call.name == method_name }
|
fetch_type(type).calls.select { |call| call.name == method_name }
|
||||||
end
|
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)
|
private def fetch_instance(object)
|
||||||
key = unique_key(object)
|
key = unique_key(object)
|
||||||
if @entries.has_key?(key)
|
if @entries.has_key?(key)
|
||||||
|
|
Loading…
Reference in a new issue