class Spectator::SpecBuilder

Overview

Progressively builds a test spec.

A stack is used to track the current example group. Adding an example or group will nest it under the group at the top of the stack.

Defined in:

spectator/spec_builder.cr

Constant Summary

Log = ::Spectator::Log.for(self)

Constructors

Instance Method Summary

Instance methods inherited from class Object

should(matcher : Spectator::Matchers::TypeMatcher(U), message = nil, *, _file = __FILE__, _line = __LINE__) forall U
should(matcher, message = nil, *, _file = __FILE__, _line = __LINE__)
should
, should_eventually(matcher, message = nil, *, _file = __FILE__, _line = __LINE__) should_eventually, should_never(matcher, message = nil, *, _file = __FILE__, _line = __LINE__) should_never, should_not(matcher : Spectator::Matchers::TypeMatcher(U), message = nil, *, _file = __FILE__, _line = __LINE__) forall U
should_not(matcher : Spectator::Matchers::NilMatcher, message = nil, *, _file = __FILE__, _line = __LINE__)
should_not(matcher, message = nil, *, _file = __FILE__, _line = __LINE__)
should_not

Constructor Detail

def self.new(config : Config) #

Creates a new spec builder. A root group is pushed onto the group stack.


[View source]

Instance Method Detail

def add_example(name, location, context_builder, metadata = nil, &block : Example -> _) : Nil #

Defines a new example. The example is added to the group currently on the top of the stack.

The name is the name or brief description of the example. This should be a string or nil. When nil, the example's name will be populated by the first expectation run inside of the test code.

The location optionally defined where the example originates in source code.

The context_builder is a proc that creates a context the test code should run in. See Context for more information.

A set of metadata can be used for filtering and modifying example behavior. For instance, adding a "pending" tag will mark the test as pending and skip execution.

A block must be provided. It will be yielded two arguments - the example created by this method, and the context argument. The return value of the block is ignored. It is expected that the test code runs when the block is called.


[View source]
def add_pending_example(name, location, metadata = nil, reason = nil) : Nil #

Defines a new pending example. The example is added to the group currently on the top of the stack.

The name is the name or brief description of the example. This should be a string or nil. When nil, the example's name will be an anonymous example reference.

The location optionally defined where the example originates in source code.

A set of metadata can be used for filtering and modifying example behavior. For instance, adding a "pending" tag will mark the test as pending and skip execution. A default reason can be given in case the user didn't provide one.


[View source]
def after_all(*args, **options) #

[View source]
def after_all(*args, **options, &) #

[View source]
def after_each(*args, **options) #

[View source]
def after_each(*args, **options, &) #

[View source]
def after_suite(*args, **kwargs) : Nil #

Registers a new "after_suite" hook. The hook will be pre-pended to the list. A new hook will be created by passing args to ExampleGroupHook.new.


[View source]
def after_suite(*args, **kwargs, &block) : Nil #

Registers a new "after_suite" hook. The hook will be pre-pended to the list. A new hook will be created by passing args to ExampleGroupHook.new.


[View source]
def around_each(*args, **options) #

[View source]
def around_each(*args, **options, &) #

[View source]
def before_all(*args, **options) #

[View source]
def before_all(*args, **options, &) #

[View source]
def before_each(*args, **options) #

[View source]
def before_each(*args, **options, &) #

[View source]
def before_suite(*args, **kwargs) : Nil #

Registers a new "before_suite" hook. The hook will be appended to the list. A new hook will be created by passing args to ExampleGroupHook.new.


[View source]
def before_suite(*args, **kwargs, &block) : Nil #

Registers a new "before_suite" hook. The hook will be appended to the list. A new hook will be created by passing args to ExampleGroupHook.new.


[View source]
def build : Spec #

Constructs the test spec. Returns the spec instance.

Raises an error if there were not symmetrical calls to #start_group and #end_group. This would indicate a logical error somewhere in Spectator or an extension of it.


[View source]
def end_group : Nil #

Completes a previously defined example group and pops it off the group stack. Be sure to call #start_group and #end_group symmetrically.


[View source]
def post_condition(*args, **options) #

[View source]
def post_condition(*args, **options, &) #

[View source]
def pre_condition(*args, **options) #

[View source]
def pre_condition(*args, **options, &) #

[View source]
def start_group(name, location = nil, metadata = nil) : Nil #

Defines a new example group and pushes it onto the group stack. Examples and groups defined after calling this method will be nested under the new group. The group will be finished and popped off the stack when #end_example is called.

The name is the name or brief description of the group. This should be a symbol when describing a type - the type name is represented as a symbol. Otherwise, a string should be used.

The location optionally defined where the group originates in source code.

A set of metadata can be used for filtering and modifying example behavior. For instance, adding a "pending" tag will mark tests as pending and skip execution.


[View source]
def start_iterative_group(collection, name, iterator = nil, location = nil, metadata = nil) : Nil #

Defines a new iterative example group and pushes it onto the group stack. Examples and groups defined after calling this method will be nested under the new group. The group will be finished and popped off the stack when #end_example is called.

The collection is the set of items to iterate over. Child nodes in this group will be executed once for every item in the collection. The name should be a string representation of collection. The iterator is an optional name given to a single item in collection.

The location optionally defined where the group originates in source code.

A set of metadata can be used for filtering and modifying example behavior. For instance, adding a "pending" tag will mark tests as pending and skip execution.


[View source]