Expect stubs not method names

Needed for argument syntax:
`expect(dbl).to receive(:foo).with(:bar)`
This commit is contained in:
Michael Miller 2019-11-29 09:25:58 -07:00
parent c710961be1
commit 8197a82ace
4 changed files with 18 additions and 12 deletions

View file

@ -25,7 +25,7 @@ module Spectator::Expectations
end
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)
matcher = Matchers::ReceiveMatcher.new(value, stub.arguments?)
to_eventually(matcher)

View file

@ -17,7 +17,7 @@ module Spectator::Mocks
end
else
@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}}")
end
end

View file

@ -62,7 +62,9 @@ module Spectator::Mocks
{% if body && !body.is_a?(Nop) %}
{{body.body}}
{% 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}}")
end
@ -77,8 +79,12 @@ module Spectator::Mocks
end
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 ::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}}")
end

View file

@ -5,7 +5,7 @@ module Spectator::Mocks
private struct Entry
getter stubs = Deque(MethodStub).new
getter calls = Deque(MethodCall).new
getter expected = Set(Symbol).new
getter expected = Set(MethodStub).new
end
@all_instances = {} of String => Entry
@ -65,17 +65,17 @@ 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) ||
fetch_type(object.class).expected.includes?(method_name)
def expected?(object, call : GenericMethodCall(T, NT)) : Bool forall T, NT
fetch_instance(object).expected.any?(&.callable?(call)) ||
fetch_type(object.class).expected.any?(&.callable?(call))
end
def expect(object, method_name : Symbol) : Nil
fetch_instance(object).expected.add(method_name)
def expect(object, stub : MethodStub) : Nil
fetch_instance(object).expected.add(stub)
end
def expect(type : T.class, method_name : Symbol) : Nil forall T
fetch_type(type).expected.add(method_name)
def expect(type : T.class, stub : MethodStub) : Nil forall T
fetch_type(type).expected.add(stub)
end
private def fetch_instance(object)