Class: Matchi::Change::To
- Inherits:
-
Object
- Object
- Matchi::Change::To
- Defined in:
- lib/matchi/change/to.rb
Overview
Final state matcher that verifies if a method returns an expected value after a change.
This matcher focuses on the final state of an object, verifying that a method call returns an expected value after executing a block of code. Unlike the full from/to matcher, it only cares about the end result, not the initial state.
Instance Method Summary collapse
-
#initialize(expected, &state) ⇒ To
constructor
Initialize the matcher with an expected new value and a state block.
-
#match? { ... } ⇒ Boolean
Checks if the state block returns the expected value after executing the provided block.
-
#to_s ⇒ String
Returns a human-readable description of the matcher.
Constructor Details
#initialize(expected, &state) ⇒ To
Initialize the matcher with an expected new value and a state block.
54 55 56 57 58 59 |
# File 'lib/matchi/change/to.rb', line 54 def initialize(expected, &state) raise ::ArgumentError, "a block must be provided" unless block_given? @expected = expected @state = state end |
Instance Method Details
#match? { ... } ⇒ Boolean
Checks if the state block returns the expected value after executing the provided block.
This method executes the provided block and then checks if the state block returns the expected value. It only cares about the final state, not any intermediate values or the initial state.
85 86 87 88 89 90 91 92 |
# File 'lib/matchi/change/to.rb', line 85 def match? raise ::ArgumentError, "a block must be provided" unless block_given? yield value_after = @state.call @expected.eql?(value_after) end |
#to_s ⇒ String
Returns a human-readable description of the matcher.
102 103 104 |
# File 'lib/matchi/change/to.rb', line 102 def to_s "change to #{@expected.inspect}" end |