Module: Cuprum::Utils::InstanceSpy
- Defined in:
- lib/cuprum/utils/instance_spy.rb
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.
Defined Under Namespace
Classes: Spy
Class Method Summary collapse
-
.clear_spies ⇒ Object
Retires all spies.
-
.spy_on(command_class) ⇒ Object
Finds or creates a spy object for the given module or class.
Instance Method Summary collapse
-
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Result
Executes the command and returns a Cuprum::Result or compatible object.
Class Method Details
.clear_spies ⇒ Object
Retires all spies.
Subsequent calls to the #call method on command instances will not be mirrored to existing spy objects.
41 42 43 44 45 |
# File 'lib/cuprum/utils/instance_spy.rb', line 41 def clear_spies Thread.current[:cuprum_instance_spies] = nil nil end |
.spy_on(command_class) ⇒ Cuprum::Utils::InstanceSpy::Spy .spy_on(command_class) {|Cuprum::Utils::InstanceSpy::Spy| ... } ⇒ nil
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.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/cuprum/utils/instance_spy.rb', line 71 def spy_on(command_class) guard_spy_class!(command_class) instrument_call! if block_given? instance_spy = assign_spy(command_class) yield instance_spy else assign_spy(command_class) end end |
Instance Method Details
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Result
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/cuprum/utils/instance_spy.rb', line 130 def call(*args, **kwargs, &block) if kwargs.empty? Cuprum::Utils::InstanceSpy.send(:call_spies_for, self, *args, &block) else # :nocov: Cuprum::Utils::InstanceSpy .send(:call_spies_for, self, *args, **kwargs, &block) # :nocov: end super end |