class

Spectator::Double

Inherits Spectator::Stubbable / Reference / Object

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.

Constants

Log = Spectator::Log.for(self)

Constructors

new(stubs : Array(Spectator::Stub) = [] of ::Spectator::Stub)

Redefinition of Spectator::Double.class#new

new

Redefinition of Spectator::Double.class#new

unsafe_construct(address : Pointer, *args, **opts) : self

Redefinition of Reference.class#unsafe_construct

Class methods

_spectator_calls

Retrieves all previously saved calls.

Source
from_json(string_or_io : String | IO, root : String)

Redefinition of Object.class#from_json

from_json(string_or_io : String | IO)

Redefinition of Object.class#from_json

pre_initialize(address : Pointer)

Redefinition of Reference.class#pre_initialize

Instance methods

!=(other)

Redefinition of Object#!=

!~(other)

Redefinition of Object#!~

==(other : self)

Redefinition of Reference#==

==(other : JSON::Any)

Redefinition of Reference#==

==(other)

Redefinition of Reference#==

===(other : JSON::Any)

Redefinition of Object#===

===(other)

Redefinition of Object#===

=~(other)

Redefinition of Object#=~

_spectator_calls

Retrieves all previously saved calls.

Source
_spectator_clear_calls

Clears all previously saved calls.

Source
_spectator_record_call(call : MethodCall) : Nil

Saves a call that was made to a stubbed method.

Source
_spectator_stub_for_method?(method : Symbol) : Bool

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

Source
colorize(r : UInt8, g : UInt8, b : UInt8)

Redefinition of Colorize::ObjectExtensions#colorize

colorize(fore : UInt8)

Redefinition of Colorize::ObjectExtensions#colorize

colorize(fore : Symbol)

Redefinition of Colorize::ObjectExtensions#colorize

colorize(fore : Color)

Redefinition of Colorize::ObjectExtensions#colorize

colorize

Redefinition of Colorize::ObjectExtensions#colorize

crystal_type_id

Redefinition of Object#crystal_type_id

dup

Redefinition of Reference#dup

hash(hasher)

Redefinition of Reference#hash

hash

Redefinition of Object#hash

in?(collection : Object) : Bool

Redefinition of Object#in?

in?(*values : Object) : Bool

Redefinition of Object#in?

inspect(io : IO) : Nil

Redefinition of Spectator::Double#inspect

inspect

Redefinition of Object#inspect

itself

Redefinition of Object#itself

not_nil!(message)

Redefinition of Object#not_nil!

not_nil!

Redefinition of Object#not_nil!

object_id

Redefinition of Reference#object_id

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

Redefinition of Object#pretty_inspect

pretty_print(pp : PrettyPrint) : Nil

Redefinition of Object#pretty_print

pretty_print(pp) : Nil

Redefinition of Reference#pretty_print

same?(other : Reference) : Bool

Redefinition of Reference#same?

same?(other : Nil)

Redefinition of Reference#same?

same?(other) : Bool

Redefinition of Spectator::Double#same?

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

Redefinition of Object#should

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

Redefinition of Object#should

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

Redefinition of Object#should_eventually

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

Redefinition of Object#should_never

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

Redefinition of Object#should_not

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

Redefinition of Object#should_not

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

Redefinition of Object#should_not

tap

Redefinition of Object#tap

to_json(io : IO) : Nil

Redefinition of Object#to_json

to_json

Redefinition of Object#to_json

to_pretty_json(indent : String = " ") : String

Redefinition of Object#to_pretty_json

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

Redefinition of Object#to_pretty_json

to_s(io : IO) : Nil

Redefinition of Spectator::Double#to_s

to_s

Redefinition of Object#to_s

try

Redefinition of Object#try

unsafe_as(type : T.class) forall T

Redefinition of Object#unsafe_as

Macros

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
Source
method_missing(call)

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

Source