class RSpec::SleepingKingStudios::Support::ValueSpy
Encapsulates the value of a method call or block, and captures a snapshot of the value at the time the spy is initialized.
@example Observing a Method
user = Person.new(name: 'Alan Bradley') value = ValueSpy.new(user, :name) value.initial_value #=> 'Alan Bradley' value.current_value #=> 'Alan Bradley' user.name = 'Ed Dillinger' value.initial_value #=> 'Alan Bradley' value.current_value #=> 'Ed Dillinger'
@example Observing a Block
value = ValueSpy.new { Person.where(virtual: false).count } value.initial_value #=> 4 value.current_value #=> 4 Person.where(name: 'Kevin Flynn').enter_grid! value.initial_value #=> 4 value.current_value #=> 3
Attributes
@return [Integer] the hash of the watched value at the time the spy was
initialized.
@return [String] the string representation of the watched value at the
time the spy was initialized
@return [Object] the watched value at the time the spy was initialized.
Public Class Methods
Source
# File lib/rspec/sleeping_king_studios/support/value_spy.rb, line 37 def initialize(receiver = nil, method_name = nil, &block) @observed_block = if block_given? block else @method_name = method_name -> { receiver.send(method_name) } end @receiver = receiver @initial_hash = current_value.hash @initial_inspect = current_value.inspect @initial_value = current_value end
@overload initialize(receiver, method_name
)
@param [Object] receiver The object to watch. @param [Symbol, String] method_name The name of the method to watch.
@overload initialize(&block)
@yield The value to watch. The block will be called each time the value is requested, and the return value of the block will be given as the current value.
Public Instance Methods
Source
# File lib/rspec/sleeping_king_studios/support/value_spy.rb, line 63 def changed? initial_value != current_value || initial_hash != current_value.hash end
Source
# File lib/rspec/sleeping_king_studios/support/value_spy.rb, line 68 def current_value @observed_block.call end
@return [Object] the watched value when current_value
is called.
Source
# File lib/rspec/sleeping_king_studios/support/value_spy.rb, line 74 def description return 'result' unless @method_name format_message end
@return [String] a human-readable representation of the watched method or
block.
Private Instance Methods
Source
# File lib/rspec/sleeping_king_studios/support/value_spy.rb, line 86 def format_message return "#{receiver}.#{method_name}" if receiver.is_a?(Module) "#{receiver.class}##{method_name}" end