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
|
@ -6,6 +6,7 @@ Spectator.describe "Deferred stub expectation DSL" do
|
||||||
# Ensure the original is never called.
|
# Ensure the original is never called.
|
||||||
stub abstract def foo : Nil
|
stub abstract def foo : Nil
|
||||||
stub abstract def foo(arg) : Nil
|
stub abstract def foo(arg) : Nil
|
||||||
|
stub abstract def value : Int32
|
||||||
end
|
end
|
||||||
|
|
||||||
let(dbl) { double(:dbl) }
|
let(dbl) { double(:dbl) }
|
||||||
|
@ -15,6 +16,11 @@ Spectator.describe "Deferred stub expectation DSL" do
|
||||||
dbl.foo
|
dbl.foo
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "returns the correct value" do
|
||||||
|
expect(dbl).to receive(:value).and_return(42)
|
||||||
|
expect(dbl.value).to eq(42)
|
||||||
|
end
|
||||||
|
|
||||||
it "matches when a message isn't received" do
|
it "matches when a message isn't received" do
|
||||||
expect(dbl).to_not receive(:foo)
|
expect(dbl).to_not receive(:foo)
|
||||||
end
|
end
|
||||||
|
@ -54,6 +60,11 @@ Spectator.describe "Deferred stub expectation DSL" do
|
||||||
fake.foo(:bar)
|
fake.foo(:bar)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "returns the correct value" do
|
||||||
|
expect(fake).to receive(:foo).and_return(42)
|
||||||
|
expect(fake.foo).to eq(42)
|
||||||
|
end
|
||||||
|
|
||||||
it "matches when a message isn't received" do
|
it "matches when a message isn't received" do
|
||||||
expect(fake).to_not receive(:foo)
|
expect(fake).to_not receive(:foo)
|
||||||
end
|
end
|
||||||
|
@ -64,7 +75,7 @@ Spectator.describe "Deferred stub expectation DSL" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "matches when a message without arguments is received" do
|
it "matches when a message without arguments is received" do
|
||||||
expect(fake).to_not receive(:foo).with(:bar)
|
expect(fake).to_not receive(:foo).with(:bar).and_return(42)
|
||||||
fake.foo
|
fake.foo
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -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 %}
|
{% 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
|
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)
|
stubbable._spectator_define_stub(stub)
|
||||||
matcher = Matchers::ReceiveMatcher.new(stub)
|
matcher = Matchers::ReceiveMatcher.new(stub)
|
||||||
to_eventually(matcher, message)
|
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 %}
|
{% 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
|
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)
|
stubbable._spectator_define_stub(stub)
|
||||||
matcher = Matchers::ReceiveMatcher.new(stub)
|
matcher = Matchers::ReceiveMatcher.new(stub)
|
||||||
to_never(matcher, message)
|
to_never(matcher, message)
|
||||||
|
|
|
@ -115,7 +115,7 @@ module Spectator
|
||||||
stub
|
stub
|
||||||
end
|
end
|
||||||
|
|
||||||
private def _spectator_stub_for_method?(method : Symbol) : Bool
|
def _spectator_stub_for_method?(method : Symbol) : Bool
|
||||||
@stubs.any? { |stub| stub.method == method }
|
@stubs.any? { |stub| stub.method == method }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ module Spectator
|
||||||
_spectator_stubs.find &.===(call)
|
_spectator_stubs.find &.===(call)
|
||||||
end
|
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 }
|
_spectator_stubs.any? { |stub| stub.method == method }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue