diff --git a/src/spectator/mocks/registry.cr b/src/spectator/mocks/registry.cr index 2791bc5..c76c1ba 100644 --- a/src/spectator/mocks/registry.cr +++ b/src/spectator/mocks/registry.cr @@ -11,11 +11,20 @@ module Spectator::Mocks @entries = {} of Key => Entry def initialize(context : TestContext) - @all_instances = context.stubs.map do |k, v| - entry = Entry.new - entry.stubs.concat(v) - {k, entry} - end.to_h + 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 + @all_instances[k] = entry + entry.stubs + end + stubs.concat(v) + end + current_context = current_context.parent? + end end def reset : Nil diff --git a/src/spectator/test_context.cr b/src/spectator/test_context.cr index fd735e2..bf2c612 100644 --- a/src/spectator/test_context.cr +++ b/src/spectator/test_context.cr @@ -3,15 +3,17 @@ require "./test_values" module Spectator class TestContext + getter! parent + getter values getter stubs : Hash(String, Deque(Mocks::MethodStub)) def initialize(@parent : TestContext?, - @hooks : ExampleHooks, - @conditions : ExampleConditions, - @values : TestValues, - @stubs : Hash(String, Deque(Mocks::MethodStub))) + @hooks : ExampleHooks, + @conditions : ExampleConditions, + @values : TestValues, + @stubs : Hash(String, Deque(Mocks::MethodStub))) @before_all_hooks_run = false @after_all_hooks_run = false end