mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Expect stubs not method names
Needed for argument syntax: `expect(dbl).to receive(:foo).with(:bar)`
This commit is contained in:
parent
c710961be1
commit
8197a82ace
4 changed files with 18 additions and 12 deletions
|
@ -25,7 +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)
|
Harness.current.mocks.expect(@actual.value, stub)
|
||||||
value = TestValue.new(stub.name, stub.to_s)
|
value = TestValue.new(stub.name, stub.to_s)
|
||||||
matcher = Matchers::ReceiveMatcher.new(value, stub.arguments?)
|
matcher = Matchers::ReceiveMatcher.new(value, stub.arguments?)
|
||||||
to_eventually(matcher)
|
to_eventually(matcher)
|
||||||
|
|
|
@ -17,7 +17,7 @@ module Spectator::Mocks
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@values.fetch({{call.name.symbolize}}) do
|
@values.fetch({{call.name.symbolize}}) do
|
||||||
return nil if ::Spectator::Harness.current.mocks.expected?(self, {{call.name.symbolize}})
|
return nil if ::Spectator::Harness.current.mocks.expected?(self, call)
|
||||||
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{call.name}}")
|
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{call.name}}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -62,7 +62,9 @@ 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}})
|
%args = ::Spectator::Mocks::GenericArguments.create({{params.splat}})
|
||||||
|
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
|
||||||
|
unless ::Spectator::Harness.current.mocks.expected?(self, %call)
|
||||||
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{name}}")
|
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{name}}")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -77,8 +79,12 @@ module Spectator::Mocks
|
||||||
end
|
end
|
||||||
|
|
||||||
macro method_missing(call)
|
macro method_missing(call)
|
||||||
|
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)
|
||||||
|
|
||||||
return self if @null
|
return self if @null
|
||||||
return self if ::Spectator::Harness.current.mocks.expected?(self, {{call.name.symbolize}})
|
return self if ::Spectator::Harness.current.mocks.expected?(self, call)
|
||||||
|
|
||||||
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,7 +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
|
getter expected = Set(MethodStub).new
|
||||||
end
|
end
|
||||||
|
|
||||||
@all_instances = {} of String => Entry
|
@all_instances = {} of String => Entry
|
||||||
|
@ -65,17 +65,17 @@ 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
|
def expected?(object, call : GenericMethodCall(T, NT)) : Bool forall T, NT
|
||||||
fetch_instance(object).expected.includes?(method_name) ||
|
fetch_instance(object).expected.any?(&.callable?(call)) ||
|
||||||
fetch_type(object.class).expected.includes?(method_name)
|
fetch_type(object.class).expected.any?(&.callable?(call))
|
||||||
end
|
end
|
||||||
|
|
||||||
def expect(object, method_name : Symbol) : Nil
|
def expect(object, stub : MethodStub) : Nil
|
||||||
fetch_instance(object).expected.add(method_name)
|
fetch_instance(object).expected.add(stub)
|
||||||
end
|
end
|
||||||
|
|
||||||
def expect(type : T.class, method_name : Symbol) : Nil forall T
|
def expect(type : T.class, stub : MethodStub) : Nil forall T
|
||||||
fetch_type(type).expected.add(method_name)
|
fetch_type(type).expected.add(stub)
|
||||||
end
|
end
|
||||||
|
|
||||||
private def fetch_instance(object)
|
private def fetch_instance(object)
|
||||||
|
|
Loading…
Reference in a new issue