Class: RSpec::SleepingKingStudios::Support::ValueSpy

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/sleeping_king_studios/support/value_spy.rb

Overview

Encapsulates the value of a method call or block, and captures a snapshot of the value at the time the spy is initialized.

Examples:

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'

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver, method_name) ⇒ ValueSpy #initialize { ... } ⇒ ValueSpy

Returns a new instance of ValueSpy.

Overloads:

  • #initialize(receiver, method_name) ⇒ ValueSpy

    Parameters:

    • receiver (Object)

      The object to watch.

    • method_name (Symbol, String)

      The name of the method to watch.

  • #initialize { ... } ⇒ ValueSpy

    Yields:

    • 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.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

Instance Attribute Details

#initial_hashInteger (readonly)

Returns the hash of the watched value at the time the spy was initialized.

Returns:

  • (Integer)

    the hash of the watched value at the time the spy was initialized.



54
55
56
# File 'lib/rspec/sleeping_king_studios/support/value_spy.rb', line 54

def initial_hash
  @initial_hash
end

#initial_inspectString (readonly)

Returns the string representation of the watched value at the time the spy was initialized.

Returns:

  • (String)

    the string representation of the watched value at the time the spy was initialized



58
59
60
# File 'lib/rspec/sleeping_king_studios/support/value_spy.rb', line 58

def initial_inspect
  @initial_inspect
end

#initial_valueObject (readonly)

Returns the watched value at the time the spy was initialized.

Returns:

  • (Object)

    the watched value at the time the spy was initialized.



61
62
63
# File 'lib/rspec/sleeping_king_studios/support/value_spy.rb', line 61

def initial_value
  @initial_value
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rspec/sleeping_king_studios/support/value_spy.rb', line 63

def changed?
  initial_value != current_value || initial_hash != current_value.hash
end

#current_valueObject

Returns the watched value when #current_value is called.

Returns:

  • (Object)

    the watched value when #current_value is called.



68
69
70
# File 'lib/rspec/sleeping_king_studios/support/value_spy.rb', line 68

def current_value
  @observed_block.call
end

#descriptionString

Returns a human-readable representation of the watched method or block.

Returns:

  • (String)

    a human-readable representation of the watched method or block.



74
75
76
77
78
# File 'lib/rspec/sleeping_king_studios/support/value_spy.rb', line 74

def description
  return 'result' unless @method_name

  format_message
end