Add default stubs from parent contexts

This commit is contained in:
Michael Miller 2019-11-10 13:22:18 -07:00
parent d2e27d1e6e
commit 6c0693f2a4
2 changed files with 20 additions and 9 deletions
src/spectator

View file

@ -11,11 +11,20 @@ module Spectator::Mocks
@entries = {} of Key => Entry @entries = {} of Key => Entry
def initialize(context : TestContext) def initialize(context : TestContext)
@all_instances = context.stubs.map do |k, v| current_context = context
while current_context
current_context.stubs.each do |k, v|
stubs = if @all_instances.has_key?(k)
@all_instances[k].stubs
else
entry = Entry.new entry = Entry.new
entry.stubs.concat(v) @all_instances[k] = entry
{k, entry} entry.stubs
end.to_h end
stubs.concat(v)
end
current_context = current_context.parent?
end
end end
def reset : Nil def reset : Nil

View file

@ -3,6 +3,8 @@ require "./test_values"
module Spectator module Spectator
class TestContext class TestContext
getter! parent
getter values getter values
getter stubs : Hash(String, Deque(Mocks::MethodStub)) getter stubs : Hash(String, Deque(Mocks::MethodStub))