Class: RSpec::SleepingKingStudios::Support::ValueSpy
- Inherits:
-
Object
- Object
- RSpec::SleepingKingStudios::Support::ValueSpy
- 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.
Instance Attribute Summary collapse
-
#initial_hash ⇒ Integer
readonly
The hash of the watched value at the time the spy was initialized.
-
#initial_inspect ⇒ String
readonly
The string representation of the watched value at the time the spy was initialized.
-
#initial_value ⇒ Object
readonly
The watched value at the time the spy was initialized.
Instance Method Summary collapse
- #changed? ⇒ Boolean
-
#current_value ⇒ Object
The watched value when #current_value is called.
-
#description ⇒ String
A human-readable representation of the watched method or block.
-
#initialize(receiver = nil, method_name = nil, &block) ⇒ ValueSpy
constructor
A new instance of ValueSpy.
Constructor Details
#initialize(receiver, method_name) ⇒ ValueSpy #initialize { ... } ⇒ ValueSpy
Returns a new instance of ValueSpy.
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_hash ⇒ Integer (readonly)
Returns 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_inspect ⇒ String (readonly)
Returns 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_value ⇒ Object (readonly)
Returns 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
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_value ⇒ Object
Returns 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 |
#description ⇒ String
Returns 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 end |