abstract class Spectator::Double

Overview

Stands in for an object for testing that a SUT calls expected methods.

Handles all messages (method calls), but only responds to those configured. Methods called that were not configured will raise UnexpectedMessage. Doubles should be defined with the #define macro.

Use #_spectator_define_stub to override behavior of a method in the double. Only methods defined in the double's type can have stubs. New methods are not defines when a stub is added that doesn't have a matching method name.

Included Modules

Extended Modules

Direct Known Subclasses

Defined in:

spectator/mocks/double.cr

Constant Summary

Log = Spectator::Log.for(self)

Constructors

Class Method Summary

Instance Method Summary

Macro Summary

Instance methods inherited from module Spectator::Stubbable

_spectator_abstract_stub_fallback(call : MethodCall, type)
_spectator_abstract_stub_fallback(call : MethodCall)
_spectator_abstract_stub_fallback
, _spectator_calls _spectator_calls, _spectator_clear_calls : Nil _spectator_clear_calls, _spectator_clear_stubs : Nil _spectator_clear_stubs, _spectator_define_stub(stub : Stub) : Nil _spectator_define_stub, _spectator_find_stub(call : MethodCall) : Stub | Nil _spectator_find_stub, _spectator_record_call(call : MethodCall) : Nil _spectator_record_call, _spectator_reset : Nil _spectator_reset, _spectator_stub_fallback(call : MethodCall, type, &)
_spectator_stub_fallback(call : MethodCall, &)
_spectator_stub_fallback
, _spectator_stub_for_method?(method : Symbol) : Bool _spectator_stub_for_method?, _spectator_stubbed_name : String _spectator_stubbed_name

Instance methods inherited from class Object

should(matcher, message = nil) should, should_eventually(matcher, message = nil) should_eventually, should_never(matcher, message = nil) should_never, should_not(matcher, message = nil) should_not

Constructor Detail

def self.new(stubs : Array(Spectator::Stub) = [] of ::Spectator::Stub) #

"Hide" existing methods and methods from ancestors by overriding them.


def self.new(**value_methods) #

"Hide" existing methods and methods from ancestors by overriding them.


Class Method Detail

def self._spectator_calls : Array(MethodCall) #
Description copied from module Spectator::Stubbable

Retrieves all previously saved calls.


[View source]
def self.from_json(string_or_io, root : String) #

"Hide" existing methods and methods from ancestors by overriding them.


def self.from_json(string_or_io) #

"Hide" existing methods and methods from ancestors by overriding them.


Instance Method Detail

def !=(other) #

"Hide" existing methods and methods from ancestors by overriding them.


def !~(other) #

"Hide" existing methods and methods from ancestors by overriding them.


def ==(other : self) #

"Hide" existing methods and methods from ancestors by overriding them.


def ==(other : JSON::Any) #

"Hide" existing methods and methods from ancestors by overriding them.


def ==(other) #

"Hide" existing methods and methods from ancestors by overriding them.


def ===(other : JSON::Any) #

"Hide" existing methods and methods from ancestors by overriding them.


def ===(other) #

"Hide" existing methods and methods from ancestors by overriding them.


def =~(other) #

"Hide" existing methods and methods from ancestors by overriding them.


def _spectator_calls : Array(Spectator::MethodCall) #
Description copied from module Spectator::Stubbable

Retrieves all previously saved calls.


[View source]
def _spectator_clear_calls : Nil #
Description copied from module Spectator::Stubbable

Clears all previously saved calls.


[View source]
def _spectator_record_call(call : MethodCall) : Nil #
Description copied from module Spectator::Stubbable

Saves a call that was made to a stubbed method.


[View source]
def _spectator_stub_for_method?(method : Symbol) : Bool #
Description copied from module Spectator::Stubbable

Utility method that looks for stubs for methods with the name specified.


[View source]
def colorize(r : UInt8, g : UInt8, b : UInt8) #

"Hide" existing methods and methods from ancestors by overriding them.


def colorize(fore : UInt8) #

"Hide" existing methods and methods from ancestors by overriding them.


def colorize(fore : Symbol) #

"Hide" existing methods and methods from ancestors by overriding them.


def colorize(fore : Color) #

"Hide" existing methods and methods from ancestors by overriding them.


def colorize : Colorize::Object #

"Hide" existing methods and methods from ancestors by overriding them.


def crystal_type_id : Int32 #

"Hide" existing methods and methods from ancestors by overriding them.


def dup #

"Hide" existing methods and methods from ancestors by overriding them.


def hash(hasher) #

"Hide" existing methods and methods from ancestors by overriding them.


def hash #

"Hide" existing methods and methods from ancestors by overriding them.


def in?(collection : Object) : Bool #

"Hide" existing methods and methods from ancestors by overriding them.


def in?(*values : Object) : Bool #

"Hide" existing methods and methods from ancestors by overriding them.


def inspect(io : IO) : Nil #

"Hide" existing methods and methods from ancestors by overriding them.


def inspect : String #

"Hide" existing methods and methods from ancestors by overriding them.


def itself #

"Hide" existing methods and methods from ancestors by overriding them.


def not_nil!(message) #

"Hide" existing methods and methods from ancestors by overriding them.


def not_nil! #

"Hide" existing methods and methods from ancestors by overriding them.


def object_id : UInt64 #

"Hide" existing methods and methods from ancestors by overriding them.


def pretty_inspect(width = 79, newline = "\n", indent = 0) : String #

"Hide" existing methods and methods from ancestors by overriding them.


def pretty_print(pp : PrettyPrint) : Nil #

"Hide" existing methods and methods from ancestors by overriding them.


def pretty_print(pp) : Nil #

"Hide" existing methods and methods from ancestors by overriding them.


def same?(other : Reference) : Bool #

"Hide" existing methods and methods from ancestors by overriding them.


def same?(other : Nil) #

"Hide" existing methods and methods from ancestors by overriding them.


def same?(other) : Bool #

"Hide" existing methods and methods from ancestors by overriding them.


def should(matcher, message = nil) #

"Hide" existing methods and methods from ancestors by overriding them.


def should_eventually(matcher, message = nil) #

"Hide" existing methods and methods from ancestors by overriding them.


def should_never(matcher, message = nil) #

"Hide" existing methods and methods from ancestors by overriding them.


def should_not(matcher, message = nil) #

"Hide" existing methods and methods from ancestors by overriding them.


def tap(&) #

"Hide" existing methods and methods from ancestors by overriding them.


def to_json(io : IO) : Nil #

"Hide" existing methods and methods from ancestors by overriding them.


def to_json : String #

"Hide" existing methods and methods from ancestors by overriding them.


def to_pretty_json(indent : String = " ") : String #

"Hide" existing methods and methods from ancestors by overriding them.


def to_pretty_json(io : IO, indent : String = " ") : Nil #

"Hide" existing methods and methods from ancestors by overriding them.


def to_s(io : IO) : Nil #

"Hide" existing methods and methods from ancestors by overriding them.


def to_s : String #

"Hide" existing methods and methods from ancestors by overriding them.


def try(&) #

"Hide" existing methods and methods from ancestors by overriding them.


def unsafe_as(type : T.class) forall T #

"Hide" existing methods and methods from ancestors by overriding them.


Macro Detail

macro define(type_name, name = nil, **value_methods, &block) #

Defines a test double type.

The type_name is the name to give the class. Instances of the double can be named by providing a name. This can be a symbol, string, or even a type. See StubbedName for details.

After the names, a collection of key-value pairs can be given to quickly define methods. Each key is the method name, and the corresponding value is the value returned by the method. These methods accept any arguments. Additionally, these methods can be overridden later with stubs.

Lastly, a block can be provided to define additional methods and stubs. The block is evaluated in the context of the double's class.

Double.define(SomeDouble, meth1: 42, meth2: "foobar") do
  stub abstract def meth3 : Symbol

  # Default implementation with a dynamic value.
  stub def meth4
    Time.utc
  end
end

[View source]
macro method_missing(call) #

Handle all methods but only respond to configured messages. Raises an UnexpectedMessage error for non-configures messages.


[View source]