mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add default stubs to TestContext
Pass default stubs into mocks registry on example startup.
This commit is contained in:
parent
a2b72eaa36
commit
14876a8e9a
8 changed files with 29 additions and 13 deletions
|
@ -27,8 +27,7 @@ module Spectator
|
|||
# The *example* argument will be the example to run.
|
||||
# The result returned from `Example#run` will be returned.
|
||||
def self.run(example : Example) : Result
|
||||
@@current = harness = new(example)
|
||||
harness.mocks.prepare(example.group.context)
|
||||
@@current = new(example)
|
||||
example.run
|
||||
ensure
|
||||
@@current = nil
|
||||
|
@ -37,7 +36,7 @@ module Spectator
|
|||
# Retrieves the current running example.
|
||||
getter example : Example
|
||||
|
||||
getter mocks = Mocks::Registry.new
|
||||
getter mocks : Mocks::Registry
|
||||
|
||||
# Retrieves the group for the current running example.
|
||||
def group
|
||||
|
@ -60,6 +59,7 @@ module Spectator
|
|||
# The example the harness is for should be passed in.
|
||||
private def initialize(@example)
|
||||
@reporter = Expectations::ExpectationReporter.new
|
||||
@mocks = Mocks::Registry.new(@example.group.context.stubs)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,8 +10,12 @@ module Spectator::Mocks
|
|||
@all_instances = {} of String => Entry
|
||||
@entries = {} of Key => Entry
|
||||
|
||||
def prepare(context : TestContext) : Nil
|
||||
# TODO
|
||||
def initialize(default_stubs)
|
||||
@all_instances = default_stubs.map do |k, v|
|
||||
entry = Entry.new
|
||||
entry.stubs.concat(v)
|
||||
{k, entry}
|
||||
end.to_h
|
||||
end
|
||||
|
||||
def reset : Nil
|
||||
|
|
|
@ -96,9 +96,8 @@ module Spectator
|
|||
@@stack.current.add_post_condition(block)
|
||||
end
|
||||
|
||||
def add_double(id : Symbol, double_type : Double.class) : Nil
|
||||
double_factory = DoubleFactory.new(double_type)
|
||||
current_group.add_double(id, double_factory)
|
||||
def add_default_stub(*args) : Nil
|
||||
@@stack.current.add_default_stub(*args)
|
||||
end
|
||||
|
||||
# Builds the entire spec and returns it as a test suite.
|
||||
|
|
|
@ -14,6 +14,7 @@ module Spectator::SpecBuilder
|
|||
@around_each_hooks = Deque(::SpectatorTest, Proc(Nil) ->).new
|
||||
@pre_conditions = Deque(TestMetaMethod).new
|
||||
@post_conditions = Deque(TestMetaMethod).new
|
||||
@default_stubs = {} of String => Deque(Mocks::MethodStub)
|
||||
|
||||
def add_child(child : Child)
|
||||
@children << child
|
||||
|
@ -47,6 +48,12 @@ module Spectator::SpecBuilder
|
|||
@post_conditions << hook
|
||||
end
|
||||
|
||||
def add_default_stub(type : T.class, stub : Mocks::MethodStub) forall T
|
||||
key = type.name
|
||||
@default_stubs[key] = Dequeue.new unless @default_stubs.has_key?(key)
|
||||
@default_stubs[key] << stub
|
||||
end
|
||||
|
||||
private def build_hooks
|
||||
ExampleHooks.new(
|
||||
@before_all_hooks.to_a,
|
||||
|
|
|
@ -7,7 +7,7 @@ module Spectator::SpecBuilder
|
|||
end
|
||||
|
||||
def build(parent_group)
|
||||
context = TestContext.new(parent_group.context, build_hooks, build_conditions, parent_group.context.values)
|
||||
context = TestContext.new(parent_group.context, build_hooks, build_conditions, parent_group.context.values, @default_stubs)
|
||||
NestedExampleGroup.new(@description, @source, parent_group, context).tap do |group|
|
||||
group.children = children.map do |child|
|
||||
child.build(group).as(ExampleComponent)
|
||||
|
|
|
@ -4,7 +4,7 @@ require "./example_group_builder"
|
|||
module Spectator::SpecBuilder
|
||||
class RootExampleGroupBuilder < ExampleGroupBuilder
|
||||
def build
|
||||
context = TestContext.new(nil, build_hooks, build_conditions, TestValues.empty)
|
||||
context = TestContext.new(nil, build_hooks, build_conditions, TestValues.empty, {} of String => Deque(Mocks::MethodStub))
|
||||
RootExampleGroup.new(context).tap do |group|
|
||||
group.children = children.map do |child|
|
||||
child.build(group).as(ExampleComponent)
|
||||
|
|
|
@ -9,7 +9,7 @@ module Spectator::SpecBuilder
|
|||
def build(parent_group)
|
||||
values = parent_group.context.values
|
||||
collection = @collection_builder.call(values)
|
||||
context = TestContext.new(parent_group.context, build_hooks, build_conditions, values)
|
||||
context = TestContext.new(parent_group.context, build_hooks, build_conditions, values, @default_stubs)
|
||||
NestedExampleGroup.new(@description, @source, parent_group, context).tap do |group|
|
||||
group.children = collection.map do |element|
|
||||
build_sub_group(group, element).as(ExampleComponent)
|
||||
|
@ -19,7 +19,7 @@ module Spectator::SpecBuilder
|
|||
|
||||
private def build_sub_group(parent_group, element)
|
||||
values = parent_group.context.values.add(@id, @description.to_s, element)
|
||||
context = TestContext.new(parent_group.context, ExampleHooks.empty, ExampleConditions.empty, values)
|
||||
context = TestContext.new(parent_group.context, ExampleHooks.empty, ExampleConditions.empty, values, {} of String => Deque(Mocks::MethodStub))
|
||||
NestedExampleGroup.new("#{@label} = #{element.inspect}", @source, parent_group, context).tap do |group|
|
||||
group.children = children.map do |child|
|
||||
child.build(group).as(ExampleComponent)
|
||||
|
|
|
@ -5,7 +5,13 @@ module Spectator
|
|||
class TestContext
|
||||
getter values
|
||||
|
||||
def initialize(@parent : TestContext?, @hooks : ExampleHooks, @conditions : ExampleConditions, @values : TestValues)
|
||||
getter stubs : Hash(String, Deque(Mocks::MethodStub))
|
||||
|
||||
def initialize(@parent : TestContext?,
|
||||
@hooks : ExampleHooks,
|
||||
@conditions : ExampleConditions,
|
||||
@values : TestValues,
|
||||
@stubs : Hash(String, Deque(Mocks::MethodStub)))
|
||||
@before_all_hooks_run = false
|
||||
@after_all_hooks_run = false
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue