Cuprum

An opinionated implementation of the Command pattern for Ruby applications. Cuprum wraps your business logic in a consistent, object-oriented interface and features status and error management, composability and control flow management.

Module: Cuprum::Utils::InstanceSpy

Parent Namespace
Cuprum::Utils
Defined In
lib/cuprum/utils/instance_spy.rb

Table Of Contents

Overview

Instruments calls to the #call method of any instance of a command class.

This can be used to unobtrusively test the functionality of code that calls a command without providing a reference to the command instance, such methods that create and call a command instance.

Examples

Observing calls to instances of a command.

spy = Cuprum::Utils::InstanceSpy.spy_on(CustomCommand)

allow(spy).to receive(:call)

CustomCommand.new.call(1, 2, 3, :four => '4')

expect(spy).to have_received(:call).with(1, 2, 3, :four => '4')

Block syntax

Cuprum::Utils::InstanceSpy.spy_on(CustomCommand) do |spy|
  allow(spy).to receive(:call)

  CustomCommand.new.call

  expect(spy).to have_received(:call)
end

Back To Top

Defined Under Namespace

Classes
Spy

Back To Top

Class Methods

.clear_spies => Object

Retires all spies.

Subsequent calls to the #call method on command instances will not be mirrored to existing spy objects.

.spy_on(command_class) => Cuprum::Utils::InstanceSpy::Spy
.spy_on(command_class, &block) => nil

Note: Calling this method for the first time will prepend the Cuprum::Utils::InstanceSpy module to Cuprum::Command.

Finds or creates a spy object for the given module or class.

Each time that the #call method is called for an object of the given type, the spy’s #call method will be invoked with the same arguments and block.

Overloads

.spy_on(command_class) => Cuprum::Utils::InstanceSpy::Spy
Returns
.spy_on(command_class, &block) => nil

Yields the instance spy to the block, and returns nil.

Yields
  • (Cuprum::Utils::InstanceSpy::Spy) — The instance spy.
Returns
  • (nil) — nil.

Parameters

Raises

Back To Top

Instance Methods

#call(*arguments, **keywords, &block) => Cuprum::Result

Executes the command and returns a Cuprum::Result or compatible object.

Each time #call is invoked, the object performs the following steps:

  1. The #process method is called, passing the arguments, keywords, and block that were passed to #call.
  2. If the value returned by #process is a Cuprum::Result or compatible object, that result is directly returned by #call.
  3. Otherwise, the value returned by #process will be wrapped in a successful result, which will be returned by #call.

Parameters

Yields

Returns

Back To Top


Back to Documentation | Versions | 1.3 | Reference | Cuprum | Cuprum::Utils