Ensure mocking doesn't change the memory

This can be important for serialization.
Cleanup names.
This commit is contained in:
Michael Miller 2022-05-15 11:27:58 -06:00
parent ac03bd439b
commit 51f133eb61
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF

View file

@ -1,9 +1,7 @@
require "../../spec_helper" require "../../spec_helper"
class Thing2 class MockedClass
def method1 getter method1 = 42
42
end
def method2 def method2
:original :original
@ -12,6 +10,10 @@ class Thing2
def method3 def method3
"original" "original"
end end
def instance_variables
[{{@type.instance_vars.map(&.name.symbolize).splat}}]
end
end end
Spectator.describe Spectator::Mock do Spectator.describe Spectator::Mock do
@ -64,24 +66,24 @@ Spectator.describe Spectator::Mock do
end end
describe "#inject" do describe "#inject" do
# For some reason, Thing2 is not visible to `inject` below when defined here. # For some reason, `inject` can't find the types.
# Thing2's definition is outside of the spec to get around this. # Their definitions are outside of the spec as a workaround.
context "with a class" do context "with a class" do
Spectator::Mock.inject(Thing2, :mock_name, method1: 123) do Spectator::Mock.inject(MockedClass, :mock_name, method1: 123) do
stub def method2 stub def method2
:stubbed :stubbed
end end
end end
let(thing) { Thing2.new } let(mock) { MockedClass.new }
it "overrides responses from methods with keyword arguments" do it "overrides responses from methods with keyword arguments" do
expect(thing.method1).to eq(123) expect(mock.method1).to eq(123)
end end
it "overrides responses from methods defined in the block" do it "overrides responses from methods defined in the block" do
expect(thing.method2).to eq(:stubbed) expect(mock.method2).to eq(:stubbed)
end end
it "allows methods to be stubbed" do it "allows methods to be stubbed" do
@ -90,11 +92,19 @@ Spectator.describe Spectator::Mock do
stub3 = Spectator::ValueStub.new(:method3, "stubbed") stub3 = Spectator::ValueStub.new(:method3, "stubbed")
aggregate_failures do aggregate_failures do
expect { thing._spectator_define_stub(stub1) }.to change { thing.method1 }.to(777) expect { mock._spectator_define_stub(stub1) }.to change { mock.method1 }.to(777)
expect { thing._spectator_define_stub(stub2) }.to change { thing.method2 }.to(:override) expect { mock._spectator_define_stub(stub2) }.to change { mock.method2 }.to(:override)
expect { thing._spectator_define_stub(stub3) }.to change { thing.method3 }.from("original").to("stubbed") expect { mock._spectator_define_stub(stub3) }.to change { mock.method3 }.from("original").to("stubbed")
end end
end end
it "doesn't change the size of an instance" do
expect(instance_sizeof(MockedClass)).to eq(8) # sizeof(Int32) + sizeof(TypeID)
end
it "doesn't affect instance variables" do
expect(mock.instance_variables).to eq([:method1])
end
end end
end end
end end