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