Record calls on injected mocks

This commit is contained in:
Michael Miller 2022-06-28 23:36:24 -06:00
parent 8aed5027aa
commit c8ec0ad02a
No known key found for this signature in database
GPG key ID: AC78B32D30CE34A2
6 changed files with 146 additions and 55 deletions

View file

@ -6,56 +6,88 @@ Spectator.describe Spectator::ReferenceMockRegistry do
let(stub) { Spectator::ValueStub.new(:test, 42) }
let(stubs) { [stub] of Spectator::Stub }
let(no_stubs) { [] of Spectator::Stub }
let(call) { Spectator::MethodCall.capture(:method2, 5) }
let(calls) { [call] }
let(no_calls) { [] of Spectator::MethodCall }
it "initially has no stubs" do
expect(registry[obj]).to be_empty
expect(registry[obj].stubs).to be_empty
end
it "initially has no calls" do
expect(registry[obj].calls).to be_empty
end
it "stores stubs for an object" do
expect { registry[obj] << stub }.to change { registry[obj] }.from(no_stubs).to(stubs)
expect { registry[obj].stubs << stub }.to change { registry[obj].stubs }.from(no_stubs).to(stubs)
end
it "stores calls for an object" do
expect { registry[obj].calls << call }.to change { registry[obj].calls }.from(no_calls).to(calls)
end
it "isolates stubs between different objects" do
obj1 = "foo"
obj2 = "bar"
registry[obj2] << Spectator::ValueStub.new(:obj2, 42)
expect { registry[obj1] << stub }.to_not change { registry[obj2] }
registry[obj2].stubs << Spectator::ValueStub.new(:obj2, 42)
expect { registry[obj1].stubs << stub }.to_not change { registry[obj2].stubs }
end
it "isolates calls between different objects" do
obj1 = "foo"
obj2 = "bar"
registry[obj2].calls << Spectator::MethodCall.capture(:method1, 42)
expect { registry[obj1].calls << call }.to_not change { registry[obj2].calls }
end
describe "#fetch" do
it "retrieves existing stubs" do
registry[obj] << stub
expect(registry.fetch(obj) { no_stubs }).to eq(stubs)
registry[obj].stubs << stub
expect(registry.fetch(obj) { no_stubs }.stubs).to eq(stubs)
end
it "stores stubs on the first retrieval" do
expect(registry.fetch(obj) { stubs }).to eq(stubs)
expect(registry.fetch(obj) { stubs }.stubs).to eq(stubs)
end
it "isolates stubs between different objects" do
obj1 = "foo"
obj2 = "bar"
registry[obj2] << Spectator::ValueStub.new(:obj2, 42)
expect { registry.fetch(obj1) { no_stubs } }.to_not change { registry[obj2] }
registry[obj2].stubs << Spectator::ValueStub.new(:obj2, 42)
expect { registry.fetch(obj1) { no_stubs }.stubs }.to_not change { registry[obj2].stubs }
end
it "isolates calls between different objects" do
obj1 = "foo"
obj2 = "bar"
registry[obj2].calls << Spectator::MethodCall.capture(:method1, 42)
expect { registry.fetch(obj1) { no_stubs }.calls }.to_not change { registry[obj2].calls }
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)
registry[obj].stubs << stub
expect { registry.delete(obj) }.to change { registry[obj].stubs }.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)
registry[obj].stubs << Spectator::ValueStub.new(:stub2, 42)
expect { registry.delete(obj) }.to change { registry.fetch(obj) { stubs }.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] }
registry[obj2].stubs << Spectator::ValueStub.new(:obj2, 42)
expect { registry.delete(obj1) }.to_not change { registry[obj2].stubs }
end
it "isolates calls between different objects" do
obj1 = "foo"
obj2 = "bar"
registry[obj2].calls << Spectator::MethodCall.capture(:method1, 42)
expect { registry.delete(obj1) }.to_not change { registry[obj2].calls }
end
end
end

View file

@ -6,56 +6,88 @@ Spectator.describe Spectator::ValueMockRegistry do
let(stub) { Spectator::ValueStub.new(:test, 5) }
let(stubs) { [stub] of Spectator::Stub }
let(no_stubs) { [] of Spectator::Stub }
let(call) { Spectator::MethodCall.capture(:method2, 5) }
let(calls) { [call] }
let(no_calls) { [] of Spectator::MethodCall }
it "initially has no stubs" do
expect(registry[obj]).to be_empty
expect(registry[obj].stubs).to be_empty
end
it "initially has no calls" do
expect(registry[obj].calls).to be_empty
end
it "stores stubs for an object" do
expect { registry[obj] << stub }.to change { registry[obj] }.from(no_stubs).to(stubs)
expect { registry[obj].stubs << stub }.to change { registry[obj].stubs }.from(no_stubs).to(stubs)
end
it "stores calls for an object" do
expect { registry[obj].calls << call }.to change { registry[obj].calls }.from(no_calls).to(calls)
end
it "isolates stubs between different objects" do
obj1 = 1
obj2 = 2
registry[obj2] << Spectator::ValueStub.new(:obj2, 42)
expect { registry[obj1] << stub }.to_not change { registry[obj2] }
registry[obj2].stubs << Spectator::ValueStub.new(:obj2, 42)
expect { registry[obj1].stubs << stub }.to_not change { registry[obj2].stubs }
end
it "isolates calls between different objects" do
obj1 = 1
obj2 = 2
registry[obj2].calls << Spectator::MethodCall.capture(:method1, 42)
expect { registry[obj1].calls << call }.to_not change { registry[obj2].calls }
end
describe "#fetch" do
it "retrieves existing stubs" do
registry[obj] << stub
expect(registry.fetch(obj) { no_stubs }).to eq(stubs)
registry[obj].stubs << stub
expect(registry.fetch(obj) { no_stubs }.stubs).to eq(stubs)
end
it "stores stubs on the first retrieval" do
expect(registry.fetch(obj) { stubs }).to eq(stubs)
expect(registry.fetch(obj) { stubs }.stubs).to eq(stubs)
end
it "isolates stubs between different objects" do
obj1 = 1
obj2 = 2
registry[obj2] << Spectator::ValueStub.new(:obj2, 42)
expect { registry.fetch(obj1) { no_stubs } }.to_not change { registry[obj2] }
registry[obj2].stubs << Spectator::ValueStub.new(:obj2, 42)
expect { registry.fetch(obj1) { no_stubs }.stubs }.to_not change { registry[obj2].stubs }
end
it "isolates calls between different objects" do
obj1 = 1
obj2 = 2
registry[obj2].calls << Spectator::MethodCall.capture(:method1, 42)
expect { registry.fetch(obj1) { no_stubs }.calls }.to_not change { registry[obj2].calls }
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)
registry[obj].stubs << stub
expect { registry.delete(obj) }.to change { registry[obj].stubs }.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)
registry[obj].stubs << Spectator::ValueStub.new(:stub2, 42)
expect { registry.delete(obj) }.to change { registry.fetch(obj) { stubs }.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] }
registry[obj2].stubs << Spectator::ValueStub.new(:obj2, 42)
expect { registry.delete(obj1) }.to_not change { registry[obj2].stubs }
end
it "isolates calls between different objects" do
obj1 = 1
obj2 = 2
registry[obj2].calls << Spectator::MethodCall.capture(:method1, 42)
expect { registry.delete(obj1) }.to_not change { registry[obj2].calls }
end
end
end