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_remove_stub(stub : Stub) : Nil _spectator_remove_stub, _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 : 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(stubs : Array(Spectator::Stub) = [] of ::Spectator::Stub) #

Redefinition of Spectator::Double.class#new


def self.new(**value_methods) #

Redefinition of Spectator::Double.class#new


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) #

Redefinition of Object.class#from_json


def self.from_json(string_or_io) #

Redefinition of Object.class#from_json


Instance Method Detail

def !=(other) #

Redefinition of Object#!=


def !~(other) #

Redefinition of Object#!~


def ==(other : self) #

Redefinition of Reference#==


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

Redefinition of Reference#==


def ==(other) #

Redefinition of Reference#==


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

Redefinition of Object#===


def ===(other) #

Redefinition of Object#===


def =~(other) #

Redefinition of Object#=~


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) #

Redefinition of Colorize::ObjectExtensions#colorize


def colorize(fore : UInt8) #

Redefinition of Colorize::ObjectExtensions#colorize


def colorize(fore : Symbol) #

Redefinition of Colorize::ObjectExtensions#colorize


def colorize(fore : Color) #

Redefinition of Colorize::ObjectExtensions#colorize


def colorize : Colorize::Object #

Redefinition of Colorize::ObjectExtensions#colorize


def crystal_type_id : Int32 #

Redefinition of Object#crystal_type_id


def dup #

Redefinition of Reference#dup


def hash(hasher) #

Redefinition of Reference#hash


def hash #

Redefinition of Object#hash


def in?(collection : Object) : Bool #

Redefinition of Object#in?


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

Redefinition of Object#in?


def inspect(io : IO) : Nil #

Redefinition of Spectator::Double#inspect


def inspect : String #

Redefinition of Object#inspect


def itself #

Redefinition of Object#itself


def not_nil!(message = nil) #

Redefinition of Object#not_nil!


def object_id : UInt64 #

Redefinition of Reference#object_id


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

Redefinition of Object#pretty_inspect


def pretty_print(pp : PrettyPrint) : Nil #

Redefinition of Object#pretty_print


def pretty_print(pp) : Nil #

Redefinition of Reference#pretty_print


def same?(other : Reference) : Bool #

Redefinition of Reference#same?


def same?(other : Nil) #

Redefinition of Reference#same?


def same?(other) : Bool #

Redefinition of Spectator::Double#same?


def should(matcher : Spectator::Matchers::TypeMatcher(U), message = nil, *, _file = __FILE__, _line = __LINE__) forall U #

Redefinition of Object#should


def should(matcher, message = nil, *, _file = __FILE__, _line = __LINE__) #

Redefinition of Object#should


def should_eventually(matcher, message = nil, *, _file = __FILE__, _line = __LINE__) #

Redefinition of Object#should_eventually


def should_never(matcher, message = nil, *, _file = __FILE__, _line = __LINE__) #

Redefinition of Object#should_never


def should_not(matcher : Spectator::Matchers::TypeMatcher(U), message = nil, *, _file = __FILE__, _line = __LINE__) forall U #

Redefinition of Object#should_not


def should_not(matcher : Spectator::Matchers::NilMatcher, message = nil, *, _file = __FILE__, _line = __LINE__) #

Redefinition of Object#should_not


def should_not(matcher, message = nil, *, _file = __FILE__, _line = __LINE__) #

Redefinition of Object#should_not


def tap(&) #

Redefinition of Object#tap


def to_json(io : IO) : Nil #

Redefinition of Object#to_json


def to_json : String #

Redefinition of Object#to_json


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

Redefinition of Object#to_pretty_json


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

Redefinition of Object#to_pretty_json


def to_s(io : IO) : Nil #

Redefinition of Spectator::Double#to_s


def to_s : String #

Redefinition of Object#to_s


def try(&) #

Redefinition of Object#try


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

Redefinition of Object#unsafe_as


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]