mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Support injecting mock functionality into modules
Add mock registry for a single module.
This commit is contained in:
parent
a3c55dfa47
commit
8f80b10fc1
3 changed files with 64 additions and 1 deletions
|
@ -205,6 +205,25 @@ Spectator.describe "Mocks Docs" do
|
|||
runnable = mock(Runnable2, command: "echo foo")
|
||||
expect(runnable.run_one).to eq("Running echo foo")
|
||||
end
|
||||
|
||||
context "Injecting Mocks" do
|
||||
module MyFileUtils
|
||||
def self.rm_rf(path)
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
inject_mock MyFileUtils do
|
||||
stub def self.rm_rf(path)
|
||||
"Simulating deletion of #{path}"
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
specify do
|
||||
expect(MyFileUtils.rm_rf("/")).to be_false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "Injecting Mocks" do
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
require "./method_call"
|
||||
require "./mocked"
|
||||
require "./mock_registry"
|
||||
require "./reference_mock_registry"
|
||||
require "./stub"
|
||||
require "./stubbed_name"
|
||||
|
@ -157,7 +158,7 @@ module Spectator
|
|||
{% elsif base == :struct %}
|
||||
@@_spectator_mock_registry = ::Spectator::ValueMockRegistry(self).new
|
||||
{% else %}
|
||||
{% raise "Unsupported base type #{base} for injecting mock" %}
|
||||
@@_spectator_mock_registry = ::Spectator::MockRegistry.new
|
||||
{% end %}
|
||||
|
||||
private class_getter _spectator_stubs : ::Array(::Spectator::Stub) = [] of ::Spectator::Stub
|
||||
|
|
43
src/spectator/mocks/mock_registry.cr
Normal file
43
src/spectator/mocks/mock_registry.cr
Normal file
|
@ -0,0 +1,43 @@
|
|||
require "./mock_registry_entry"
|
||||
require "./stub"
|
||||
|
||||
module Spectator
|
||||
# Stores collections of stubs for mocked types.
|
||||
#
|
||||
# This type is intended for all mocked modules that have functionality "injected."
|
||||
# That is, the type itself has mock functionality bolted on.
|
||||
# Adding instance members should be avoided, for instance, it could mess up serialization.
|
||||
class MockRegistry
|
||||
@entry : MockRegistryEntry?
|
||||
|
||||
# Retrieves all stubs.
|
||||
def [](_object = nil)
|
||||
@entry.not_nil!
|
||||
end
|
||||
|
||||
# Retrieves all stubs.
|
||||
def []?(_object = nil)
|
||||
@entry
|
||||
end
|
||||
|
||||
# Retrieves all stubs.
|
||||
#
|
||||
# Yields to the block on the first retrieval.
|
||||
# This allows a mock to populate the registry with initial stubs.
|
||||
def fetch(object : Reference, & : -> Array(Stub))
|
||||
entry = @entry
|
||||
if entry.nil?
|
||||
entry = MockRegistryEntry.new
|
||||
entry.stubs = yield
|
||||
@entry = entry
|
||||
else
|
||||
entry
|
||||
end
|
||||
end
|
||||
|
||||
# Clears all stubs defined for a mocked object.
|
||||
def delete(object : Reference) : Nil
|
||||
@entry = nil
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue