mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Allow method calls with unconstrained arguments
Workaround for the expect-receive DSL syntax to allow methods to be called without matching arguments.
This commit is contained in:
parent
c91e288f61
commit
6e57a1c44a
4 changed files with 40 additions and 3 deletions
|
@ -147,6 +147,19 @@ module Spectator
|
|||
{% raise "The syntax `expect(...).to_eventually receive(...)` requires the expression passed to `expect` be stubbable (a mock or double)" unless T < ::Spectator::Stubbable || T < ::Spectator::StubbedType %}
|
||||
|
||||
stubbable = @expression.value
|
||||
unless stubbable._spectator_stub_for_method?(stub.method)
|
||||
# Add stub without an argument constraint.
|
||||
# Avoids confusing logic like this:
|
||||
# ```
|
||||
# expect(dbl).to receive(:foo).with(:bar)
|
||||
# dbl.foo(:baz)
|
||||
# ```
|
||||
# Notice that `#foo` is called, but with different arguments.
|
||||
# Normally this would raise an error, but that should be prevented.
|
||||
unconstrained_stub = stub.with(Arguments.any)
|
||||
stubbable._spectator_define_stub(unconstrained_stub)
|
||||
end
|
||||
|
||||
stubbable._spectator_define_stub(stub)
|
||||
matcher = Matchers::ReceiveMatcher.new(stub)
|
||||
to_eventually(matcher, message)
|
||||
|
@ -164,6 +177,19 @@ module Spectator
|
|||
{% raise "The syntax `expect(...).to_never receive(...)` requires the expression passed to `expect` be stubbable (a mock or double)" unless T < ::Spectator::Stubbable || T < ::Spectator::StubbedType %}
|
||||
|
||||
stubbable = @expression.value
|
||||
unless stubbable._spectator_stub_for_method?(stub.method)
|
||||
# Add stub without an argument constraint.
|
||||
# Avoids confusing logic like this:
|
||||
# ```
|
||||
# expect(dbl).to receive(:foo).with(:bar)
|
||||
# dbl.foo(:baz)
|
||||
# ```
|
||||
# Notice that `#foo` is called, but with different arguments.
|
||||
# Normally this would raise an error, but that should be prevented.
|
||||
unconstrained_stub = stub.with(Arguments.any)
|
||||
stubbable._spectator_define_stub(unconstrained_stub)
|
||||
end
|
||||
|
||||
stubbable._spectator_define_stub(stub)
|
||||
matcher = Matchers::ReceiveMatcher.new(stub)
|
||||
to_never(matcher, message)
|
||||
|
|
|
@ -115,7 +115,7 @@ module Spectator
|
|||
stub
|
||||
end
|
||||
|
||||
private def _spectator_stub_for_method?(method : Symbol) : Bool
|
||||
def _spectator_stub_for_method?(method : Symbol) : Bool
|
||||
@stubs.any? { |stub| stub.method == method }
|
||||
end
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ module Spectator
|
|||
_spectator_stubs.find &.===(call)
|
||||
end
|
||||
|
||||
private def _spectator_stub_for_method?(method : Symbol) : Bool
|
||||
def _spectator_stub_for_method?(method : Symbol) : Bool
|
||||
_spectator_stubs.any? { |stub| stub.method == method }
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue