Workaround absolute path requirement for mock injection

This commit is contained in:
Michael Miller 2022-07-13 12:23:28 -06:00
parent ac3b322900
commit 14d8c046f0
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
2 changed files with 7 additions and 6 deletions

View file

@ -404,7 +404,7 @@ Spectator.describe Spectator::Mock do
describe "#inject" do describe "#inject" do
context "with a class" do context "with a class" do
class MockedClass class ::MockedClass
getter _spectator_invocations = [] of Symbol getter _spectator_invocations = [] of Symbol
getter method1 do getter method1 do
@ -427,7 +427,7 @@ Spectator.describe Spectator::Mock do
end end
end end
Spectator::Mock.inject(:class, MockedClass, :mock_name, method1: 123) do Spectator::Mock.inject(:class, ::MockedClass, :mock_name, method1: 123) do
stub def method2 stub def method2
:stubbed :stubbed
end end
@ -511,7 +511,7 @@ Spectator.describe Spectator::Mock do
end end
context "with a struct" do context "with a struct" do
struct MockedStruct struct ::MockedStruct
# Using a class variable instead of an instance variable to prevent mutability problems with stub lookup. # Using a class variable instead of an instance variable to prevent mutability problems with stub lookup.
class_getter _spectator_invocations = [] of Symbol class_getter _spectator_invocations = [] of Symbol
@ -537,7 +537,7 @@ Spectator.describe Spectator::Mock do
end end
end end
Spectator::Mock.inject(:struct, MockedStruct, :mock_name, method1: 123) do Spectator::Mock.inject(:struct, ::MockedStruct, :mock_name, method1: 123) do
stub def method2 stub def method2
:stubbed :stubbed
end end
@ -608,7 +608,7 @@ Spectator.describe Spectator::Mock do
end end
context "class method stubs" do context "class method stubs" do
class Thing class ::Thing
def self.foo def self.foo
:original :original
end end
@ -622,7 +622,7 @@ Spectator.describe Spectator::Mock do
end end
end end
Spectator::Mock.inject(:class, Thing) do Spectator::Mock.inject(:class, ::Thing) do
stub def self.foo stub def self.foo
:stub :stub
end end

View file

@ -96,6 +96,7 @@ module Spectator
# The original method functionality will still be accessible, but pass through mock code first. # The original method functionality will still be accessible, but pass through mock code first.
# *base* is the keyword for the type being defined - class or struct. # *base* is the keyword for the type being defined - class or struct.
# *type_name* is the name of the type to inject mock functionality into. # *type_name* is the name of the type to inject mock functionality into.
# This _must_ be full, resolvable path to the type.
# An optional *name* of the mock can be provided. # An optional *name* of the mock can be provided.
# Any key-value pairs provided with *value_methods* are used as initial stubs for the mocked type. # Any key-value pairs provided with *value_methods* are used as initial stubs for the mocked type.
# #