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

@ -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: