mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Ensure stubs defined with allow syntax are cleared
This commit is contained in:
parent
2516803b0d
commit
090c95b162
3 changed files with 29 additions and 0 deletions
|
@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
## [Unreleased]
|
||||
### Fixed
|
||||
- Clear stubs defined with `expect().to receive()` syntax after test finishes to prevent leakage between tests.
|
||||
- Ensure stubs defined with `allow().to receive()` syntax are cleared after test finishes when used inside a test (another leakage).
|
||||
|
||||
### Removed
|
||||
- Removed support for stubbing undefined (untyped) methods in lazy doubles. Avoids possible segfault.
|
||||
|
|
|
@ -9,5 +9,31 @@ Spectator.describe Spectator::Allow do
|
|||
it "applies a stub" do
|
||||
expect { alw.to(stub) }.to change { dbl.foo }.from(42).to(123)
|
||||
end
|
||||
|
||||
context "leak" do
|
||||
class Thing
|
||||
def foo
|
||||
42
|
||||
end
|
||||
end
|
||||
|
||||
mock Thing
|
||||
|
||||
getter(thing : Thing) { mock(Thing) }
|
||||
|
||||
# Workaround type restrictions requiring a constant.
|
||||
def fake
|
||||
class_mock(Thing).cast(thing)
|
||||
end
|
||||
|
||||
specify do
|
||||
expect { allow(fake).to(stub) }.to change { fake.foo }.from(42).to(123)
|
||||
end
|
||||
|
||||
# This example must be run after the previous (random order may break this).
|
||||
it "clears the stub after the example completes" do
|
||||
expect { fake.foo }.to eq(42)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
require "../harness"
|
||||
require "./stub"
|
||||
require "./stubbable"
|
||||
require "./stubbed_type"
|
||||
|
@ -21,6 +22,7 @@ module Spectator
|
|||
# Applies a stub to the targeted stubbable object.
|
||||
def to(stub : Stub) : Nil
|
||||
@target._spectator_define_stub(stub)
|
||||
Harness.current?.try &.cleanup { @target._spectator_remove_stub(stub) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue