Clear stubs defined with `expect().to receive()` syntax after test finishes

This commit is contained in:
Michael Miller 2022-10-09 13:57:28 -06:00
parent 25b9931002
commit 5c910e5a85
No known key found for this signature in database
GPG Key ID: 32B47AE8F388A1FF
3 changed files with 25 additions and 6 deletions

View File

@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Fixed
- Clear stubs defined with `expect().to receive()` syntax after test finishes to prevent leakage between tests.
### Removed
- Removed support for stubbing undefined (untyped) methods in lazy doubles. Avoids possible segfault.

View File

@ -146,9 +146,9 @@ Spectator.describe "Mocks Docs" do
inst.something
end
it "leaks stubs to other examples" do
it "reverts to default stub for other examples" do
inst = mock(MyStruct)
expect(inst.something).to eq(7) # Previous stub was leaked.
expect(inst.something).to eq(5) # Default stub used instead of original behavior.
end
end
end

View File

@ -160,9 +160,17 @@ module Spectator
stubbable._spectator_define_stub(unconstrained_stub)
end
# Apply the stub that is expected to be called.
stubbable._spectator_define_stub(stub)
matcher = Matchers::ReceiveMatcher.new(stub)
to_eventually(matcher, message)
# Check if the stub was invoked after the test completes.
Harness.current.defer do
matcher = Matchers::ReceiveMatcher.new(stub)
to(matcher, message)
ensure
# Prevent leaking stubs between tests.
stubbable._spectator_remove_stub(stub)
end
end
# Asserts that some criteria defined by the matcher is eventually satisfied.
@ -190,9 +198,17 @@ module Spectator
stubbable._spectator_define_stub(unconstrained_stub)
end
# Apply the stub that could be called in case it is.
stubbable._spectator_define_stub(stub)
matcher = Matchers::ReceiveMatcher.new(stub)
to_never(matcher, message)
# Check if the stub was invoked after the test completes.
Harness.current.defer do
matcher = Matchers::ReceiveMatcher.new(stub)
to_not(matcher, message)
ensure
# Prevent leaking stubs between tests.
stubbable._spectator_remove_stub(stub)
end
end
# :ditto: