Add delete method to clear stubs for a mock

This commit is contained in:
Michael Miller 2022-05-15 16:19:55 -06:00
parent 847fd38534
commit a39b27387c
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
4 changed files with 58 additions and 6 deletions

View file

@ -4,6 +4,7 @@ Spectator.describe Spectator::ReferenceMockRegistry do
subject(registry) { described_class.new } subject(registry) { described_class.new }
let(obj) { "foobar" } let(obj) { "foobar" }
let(stub) { Spectator::ValueStub.new(:test, 42) } let(stub) { Spectator::ValueStub.new(:test, 42) }
let(stubs) { [stub] of Spectator::Stub }
let(no_stubs) { [] of Spectator::Stub } let(no_stubs) { [] of Spectator::Stub }
it "initially has no stubs" do it "initially has no stubs" do
@ -11,7 +12,7 @@ Spectator.describe Spectator::ReferenceMockRegistry do
end end
it "stores stubs for an object" do it "stores stubs for an object" do
expect { registry[obj] << stub }.to change { registry[obj] }.from(no_stubs).to([stub]) expect { registry[obj] << stub }.to change { registry[obj] }.from(no_stubs).to(stubs)
end end
it "isolates stubs between different objects" do it "isolates stubs between different objects" do
@ -24,11 +25,11 @@ Spectator.describe Spectator::ReferenceMockRegistry do
describe "#fetch" do describe "#fetch" do
it "retrieves existing stubs" do it "retrieves existing stubs" do
registry[obj] << stub registry[obj] << stub
expect(registry.fetch(obj) { no_stubs }).to eq([stub]) expect(registry.fetch(obj) { no_stubs }).to eq(stubs)
end end
it "stores stubs on the first retrieval" do it "stores stubs on the first retrieval" do
expect(registry.fetch(obj) { [stub] of Spectator::Stub }).to eq([stub]) expect(registry.fetch(obj) { stubs }).to eq(stubs)
end end
it "isolates stubs between different objects" do it "isolates stubs between different objects" do
@ -38,4 +39,23 @@ Spectator.describe Spectator::ReferenceMockRegistry do
expect { registry.fetch(obj1) { no_stubs } }.to_not change { registry[obj2] } expect { registry.fetch(obj1) { no_stubs } }.to_not change { registry[obj2] }
end end
end end
describe "#delete" do
it "clears stubs for an object" do
registry[obj] << stub
expect { registry.delete(obj) }.to change { registry[obj] }.from(stubs).to(no_stubs)
end
it "doesn't clear initial stubs provided with #fetch" do
registry[obj] << Spectator::ValueStub.new(:stub2, 42)
expect { registry.delete(obj) }.to change { registry.fetch(obj) { stubs } }.to(stubs)
end
it "isolates stubs between different objects" do
obj1 = "foo"
obj2 = "bar"
registry[obj2] << Spectator::ValueStub.new(:obj2, 42)
expect { registry.delete(obj1) }.to_not change { registry[obj2] }
end
end
end end

View file

@ -4,6 +4,7 @@ Spectator.describe Spectator::ValueMockRegistry do
subject(registry) { Spectator::ValueMockRegistry(Int32).new } subject(registry) { Spectator::ValueMockRegistry(Int32).new }
let(obj) { 42 } let(obj) { 42 }
let(stub) { Spectator::ValueStub.new(:test, 5) } let(stub) { Spectator::ValueStub.new(:test, 5) }
let(stubs) { [stub] of Spectator::Stub }
let(no_stubs) { [] of Spectator::Stub } let(no_stubs) { [] of Spectator::Stub }
it "initially has no stubs" do it "initially has no stubs" do
@ -11,7 +12,7 @@ Spectator.describe Spectator::ValueMockRegistry do
end end
it "stores stubs for an object" do it "stores stubs for an object" do
expect { registry[obj] << stub }.to change { registry[obj] }.from(no_stubs).to([stub]) expect { registry[obj] << stub }.to change { registry[obj] }.from(no_stubs).to(stubs)
end end
it "isolates stubs between different objects" do it "isolates stubs between different objects" do
@ -24,11 +25,11 @@ Spectator.describe Spectator::ValueMockRegistry do
describe "#fetch" do describe "#fetch" do
it "retrieves existing stubs" do it "retrieves existing stubs" do
registry[obj] << stub registry[obj] << stub
expect(registry.fetch(obj) { no_stubs }).to eq([stub]) expect(registry.fetch(obj) { no_stubs }).to eq(stubs)
end end
it "stores stubs on the first retrieval" do it "stores stubs on the first retrieval" do
expect(registry.fetch(obj) { [stub] of Spectator::Stub }).to eq([stub]) expect(registry.fetch(obj) { stubs }).to eq(stubs)
end end
it "isolates stubs between different objects" do it "isolates stubs between different objects" do
@ -38,4 +39,23 @@ Spectator.describe Spectator::ValueMockRegistry do
expect { registry.fetch(obj1) { no_stubs } }.to_not change { registry[obj2] } expect { registry.fetch(obj1) { no_stubs } }.to_not change { registry[obj2] }
end end
end end
describe "#delete" do
it "clears stubs for an object" do
registry[obj] << stub
expect { registry.delete(obj) }.to change { registry[obj] }.from(stubs).to(no_stubs)
end
it "doesn't clear initial stubs provided with #fetch" do
registry[obj] << Spectator::ValueStub.new(:stub2, 42)
expect { registry.delete(obj) }.to change { registry.fetch(obj) { stubs } }.to(stubs)
end
it "isolates stubs between different objects" do
obj1 = 1
obj2 = 2
registry[obj2] << Spectator::ValueStub.new(:obj2, 42)
expect { registry.delete(obj1) }.to_not change { registry[obj2] }
end
end
end end

View file

@ -34,5 +34,11 @@ module Spectator
@object_stubs[key] = yield @object_stubs[key] = yield
end end
end end
# Clears all stubs defined for a mocked object.
def delete(object : Reference) : Nil
key = Box.box(object)
@object_stubs.delete(key)
end
end end
end end

View file

@ -39,6 +39,12 @@ module Spectator
end end
end end
# Clears all stubs defined for a mocked object.
def delete(object : T) : Nil
key = value_bytes(object)
@object_stubs.delete(key)
end
# Extracts heap-managed bytes for a value. # Extracts heap-managed bytes for a value.
# #
# Strings are used because a string pool is used. # Strings are used because a string pool is used.