mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add functionality to clear stubs
This commit is contained in:
parent
fdac99d122
commit
c98442e0ed
7 changed files with 99 additions and 12 deletions
|
@ -315,4 +315,15 @@ Spectator.describe Spectator::Double do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#_spectator_clear_stubs" do
|
||||
subject(dbl) { FooBarDouble.new }
|
||||
let(stub) { Spectator::ValueStub.new(:foo, 5) }
|
||||
|
||||
before_each { dbl._spectator_define_stub(stub) }
|
||||
|
||||
it "removes previously defined stubs" do
|
||||
expect { dbl._spectator_clear_stubs }.to change { dbl.foo }.from(5).to(42)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -248,4 +248,24 @@ Spectator.describe Spectator::LazyDouble do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#_spectator_clear_stubs" do
|
||||
subject(dbl) { Spectator::LazyDouble.new(foo: 42, bar: "baz") }
|
||||
let(stub) { Spectator::ValueStub.new(:foo, 5) }
|
||||
|
||||
before_each { dbl._spectator_define_stub(stub) }
|
||||
|
||||
it "removes previously defined stubs" do
|
||||
expect { dbl._spectator_clear_stubs }.to change { dbl.foo }.from(5).to(42)
|
||||
end
|
||||
|
||||
it "raises on methods without an 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 raise_error(Spectator::UnexpectedMessage, /baz/)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -33,6 +33,10 @@ struct MockedStruct
|
|||
end
|
||||
|
||||
Spectator.describe Spectator::Mock do
|
||||
let(stub1) { Spectator::ValueStub.new(:method1, 777) }
|
||||
let(stub2) { Spectator::ValueStub.new(:method2, :override) }
|
||||
let(stub3) { Spectator::ValueStub.new(:method3, "stubbed") }
|
||||
|
||||
describe "#define_subclass" do
|
||||
class Thing
|
||||
def method1
|
||||
|
@ -69,16 +73,25 @@ Spectator.describe Spectator::Mock do
|
|||
end
|
||||
|
||||
it "allows methods to be stubbed" do
|
||||
stub1 = Spectator::ValueStub.new(:method1, 777)
|
||||
stub2 = Spectator::ValueStub.new(:method2, :override)
|
||||
stub3 = Spectator::ValueStub.new(:method3, "stubbed")
|
||||
|
||||
aggregate_failures do
|
||||
expect { mock._spectator_define_stub(stub1) }.to change { mock.method1 }.to(777)
|
||||
expect { mock._spectator_define_stub(stub2) }.to change { mock.method2 }.to(:override)
|
||||
expect { mock._spectator_define_stub(stub3) }.to change { mock.method3 }.from("original").to("stubbed")
|
||||
end
|
||||
end
|
||||
|
||||
it "can clear stubs" do
|
||||
mock._spectator_define_stub(stub1)
|
||||
mock._spectator_define_stub(stub2)
|
||||
mock._spectator_define_stub(stub3)
|
||||
|
||||
mock._spectator_clear_stubs
|
||||
aggregate_failures do
|
||||
expect(mock.method1).to eq(123)
|
||||
expect(mock.method2).to eq(:stubbed)
|
||||
expect(mock.method3).to eq("original")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#inject" do
|
||||
|
@ -94,6 +107,9 @@ Spectator.describe Spectator::Mock do
|
|||
|
||||
let(mock) { MockedClass.new }
|
||||
|
||||
# Necessary to clear stubs to prevent leakages between tests.
|
||||
after_each { mock._spectator_clear_stubs }
|
||||
|
||||
it "overrides responses from methods with keyword arguments" do
|
||||
expect(mock.method1).to eq(123)
|
||||
end
|
||||
|
@ -103,10 +119,6 @@ Spectator.describe Spectator::Mock do
|
|||
end
|
||||
|
||||
it "allows methods to be stubbed" do
|
||||
stub1 = Spectator::ValueStub.new(:method1, 777)
|
||||
stub2 = Spectator::ValueStub.new(:method2, :override)
|
||||
stub3 = Spectator::ValueStub.new(:method3, "stubbed")
|
||||
|
||||
aggregate_failures do
|
||||
expect { mock._spectator_define_stub(stub1) }.to change { mock.method1 }.to(777)
|
||||
expect { mock._spectator_define_stub(stub2) }.to change { mock.method2 }.to(:override)
|
||||
|
@ -114,6 +126,19 @@ Spectator.describe Spectator::Mock do
|
|||
end
|
||||
end
|
||||
|
||||
it "can clear stubs" do
|
||||
mock._spectator_define_stub(stub1)
|
||||
mock._spectator_define_stub(stub2)
|
||||
mock._spectator_define_stub(stub3)
|
||||
|
||||
mock._spectator_clear_stubs
|
||||
aggregate_failures do
|
||||
expect(mock.method1).to eq(123)
|
||||
expect(mock.method2).to eq(:stubbed)
|
||||
expect(mock.method3).to eq("original")
|
||||
end
|
||||
end
|
||||
|
||||
it "doesn't change the size of an instance" do
|
||||
expect(instance_sizeof(MockedClass)).to eq(8) # sizeof(Int32) + sizeof(TypeID)
|
||||
end
|
||||
|
@ -132,6 +157,9 @@ Spectator.describe Spectator::Mock do
|
|||
|
||||
let(mock) { MockedStruct.new }
|
||||
|
||||
# Necessary to clear stubs to prevent leakages between tests.
|
||||
after_each { mock._spectator_clear_stubs }
|
||||
|
||||
it "overrides responses from methods with keyword arguments" do
|
||||
expect(mock.method1).to eq(123)
|
||||
end
|
||||
|
@ -141,10 +169,6 @@ Spectator.describe Spectator::Mock do
|
|||
end
|
||||
|
||||
it "allows methods to be stubbed" do
|
||||
stub1 = Spectator::ValueStub.new(:method1, 777)
|
||||
stub2 = Spectator::ValueStub.new(:method2, :override)
|
||||
stub3 = Spectator::ValueStub.new(:method3, "stubbed")
|
||||
|
||||
aggregate_failures do
|
||||
expect { mock._spectator_define_stub(stub1) }.to change { mock.method1 }.to(777)
|
||||
expect { mock._spectator_define_stub(stub2) }.to change { mock.method2 }.to(:override)
|
||||
|
|
|
@ -276,4 +276,24 @@ Spectator.describe Spectator::NullDouble do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#_spectator_clear_stubs" do
|
||||
subject(dbl) { FooBarDouble.new }
|
||||
let(stub) { Spectator::ValueStub.new(:foo, 5) }
|
||||
|
||||
before_each { dbl._spectator_define_stub(stub) }
|
||||
|
||||
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
|
||||
end
|
||||
|
|
|
@ -95,6 +95,11 @@ module Spectator
|
|||
@stubs.unshift(stub)
|
||||
end
|
||||
|
||||
protected def _spectator_clear_stubs : Nil
|
||||
Log.debug { "Clearing stubs for #{_spectator_stubbed_name}" }
|
||||
@stubs.clear
|
||||
end
|
||||
|
||||
private def _spectator_find_stub(call : MethodCall) : Stub?
|
||||
Log.debug { "Finding stub for #{call}" }
|
||||
stub = @stubs.find &.===(call)
|
||||
|
|
|
@ -12,6 +12,10 @@ module Spectator
|
|||
_spectator_stubs.unshift(stub)
|
||||
end
|
||||
|
||||
def _spectator_clear_stubs : Nil
|
||||
_spectator_stubs.clear
|
||||
end
|
||||
|
||||
private def _spectator_find_stub(call : ::Spectator::MethodCall) : ::Spectator::Stub?
|
||||
_spectator_stubs.find &.===(call)
|
||||
end
|
||||
|
|
|
@ -28,6 +28,9 @@ module Spectator
|
|||
# Defines a stub to change the behavior of a method.
|
||||
abstract def _spectator_define_stub(stub : Stub) : Nil
|
||||
|
||||
# Clears all previously defined stubs.
|
||||
abstract def _spectator_clear_stubs : Nil
|
||||
|
||||
# Method called when a stub isn't found.
|
||||
#
|
||||
# The received message is captured in *call*.
|
||||
|
|
Loading…
Reference in a new issue