Disallow stubs for undefined methods in null double

Any method_missing invocation should return self.
This commit is contained in:
Michael Miller 2022-07-14 13:37:09 -06:00
parent 7dd77a2096
commit 827b69483b
No known key found for this signature in database
GPG Key ID: 32B47AE8F388A1FF
2 changed files with 4 additions and 27 deletions

View File

@ -216,12 +216,12 @@ Spectator.describe Spectator::NullDouble do
expect(dbl.foo("foobar")).to eq("bar")
end
it "raises self when constraint unsatisfied" do
it "raises when constraint unsatisfied" do
expect { dbl.foo("baz") }.to raise_error(Spectator::UnexpectedMessage, /foo/)
end
it "raises self when argument count doesn't match" do
expect { dbl.foo }.to raise_error(Spectator::UnexpectedMessage, /foo/)
it "returns self when argument count doesn't match" do
expect(dbl.foo).to be(dbl)
end
it "ignores the block argument if not in the constraint" do
@ -406,15 +406,6 @@ Spectator.describe Spectator::NullDouble do
it "removes previously defined stubs" do
expect { dbl._spectator_clear_stubs }.to change { dbl.foo }.from(5).to(42)
end
it "defaults to returning itself for methods with no implementation" do
stub = Spectator::ValueStub.new(:baz, :xyz)
dbl._spectator_define_stub(stub)
expect(dbl.baz).to eq(:xyz)
dbl._spectator_clear_stubs
expect(dbl.baz).to be(dbl)
end
end
describe "#_spectator_calls" do

View File

@ -58,21 +58,7 @@ module Spectator
%call = ::Spectator::MethodCall.new({{call.name.symbolize}}, %args)
_spectator_record_call(%call)
# Attempt to find a stub that satisfies the method call and arguments.
if %stub = _spectator_find_stub(%call)
# A method that was not defined during initialization was stubbed.
# Even though all stubs will have a #call method, the compiler doesn't seem to agree.
# Assert that it will (this should never fail).
raise TypeCastError.new("Stub has no value") unless %stub.responds_to?(:call)
# Return the value of the stub as-is.
# Might want to give a warning here, as this may produce a "bloated" union of all known stub types.
%stub.call(%call)
else
# A stub wasn't found, invoke the fallback logic.
# Message received for a methods that isn't stubbed nor defined when initialized.
_spectator_abstract_stub_fallback(%call)
end
self
end
end
end