class RSpec::SleepingKingStudios::Matchers::Core::HaveChangedMatcher
Matcher for testing the change in a value.
@since 2.4.0
Public Class Methods
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 17 def initialize super @expected_initial_value = DEFAULT_VALUE @expected_current_value = DEFAULT_VALUE end
Public Instance Methods
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 32 def by(difference) @expected_difference = difference self end
Creates an difference expectation between the initial and current values. The matcher will subtract the current value from the initial value and compare the result with the specified value.
@param [Object] difference The expected difference between the initial
value and the current value.
@return [HaveChangedMatcher] the matcher instance.
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 39 def description desc = 'have changed' unless @expected_initial_value == DEFAULT_VALUE desc << " from #{@expected_initial_value.inspect}" end if @expected_difference desc << " by #{@expected_difference.inspect}" end unless @expected_current_value == DEFAULT_VALUE desc << " to #{@expected_current_value.inspect}" end desc end
(see BaseMatcher#description
)
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 58 def does_not_match?(actual) @actual = actual unless actual.is_a?(RSpec::SleepingKingStudios::Support::ValueSpy) raise ArgumentError, 'You must pass a value spy to `expect`.' end unless @expected_current_value == DEFAULT_VALUE raise NotImplementedError, "`expect().not_to have_changed().to()` is not supported" end if @expected_difference raise NotImplementedError, "`expect().not_to have_changed().by()` is not supported" end match_initial_value? && !value_has_changed? end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 79 def failure_message unless @match_initial_value.nil? || @match_initial_value return "expected #{value_spy.description} to have initially " \ "been #{@expected_initial_value.inspect}, but was " \ "#{value_spy.initial_inspect}" end message = "expected #{value_spy.description} to have changed" if @expected_difference message << " by #{@expected_difference.inspect}" end unless @expected_current_value == DEFAULT_VALUE message << " to #{@expected_current_value.inspect}" end unless @match_difference.nil? || @match_difference return message << ", but was changed by #{@actual_difference.inspect}" end unless @match_current_value.nil? || @match_current_value return message << ", but is now #{current_value.inspect}" end message << ", but is still #{current_value.inspect}" message end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 110 def failure_message_when_negated unless @match_initial_value.nil? || @match_initial_value return "expected #{value_spy.description} to have initially " \ "been #{@expected_initial_value.inspect}, but was " \ "#{value_spy.initial_inspect}" end message = "expected #{value_spy.description} not to have changed" message << ", but did change from #{value_spy.initial_inspect} to " << current_value.inspect message end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 132 def from(value) @expected_initial_value = value self end
Creates an expectation on the initial value. The matcher will compare the initial value from the value spy with the specified value.
@param [Object] value The expected initial value.
@return [HaveChangedMatcher] the matcher instance.
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 146 def matches?(actual) super unless actual.is_a?(RSpec::SleepingKingStudios::Support::ValueSpy) raise ArgumentError, 'You must pass a value spy to `expect`.' end match_initial_value? && value_has_changed? && match_current_value? && match_difference? end
Checks if the observed value has changed.
@param [RSpec::SleepingKingStudios::Support::ValueSpy] actual The
observed value.
@return [Boolean] True if the observed value has changed, otherwise false.
@raise ArgumentError unless the actual object is a value spy.
RSpec::SleepingKingStudios::Matchers::BaseMatcher#matches?
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 165 def to(value) @expected_current_value = value self end
Creates an expectation on the current value. The matcher will compare the current value from the value spy with the specified value.
@param [Object] value The expected current value.
@return [HaveChangedMatcher] the matcher instance.
Private Instance Methods
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 175 def current_value value_spy.current_value end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 179 def initial_value value_spy.initial_value end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 183 def match_current_value? return @match_current_value unless @match_current_value.nil? if @expected_current_value == DEFAULT_VALUE return @match_current_value = true end @match_current_value = RSpec::Support::FuzzyMatcher.values_match?( current_value, @expected_current_value ) end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 196 def match_difference? return @match_difference unless @match_difference.nil? return @match_difference = true unless @expected_difference @actual_difference = current_value - initial_value @match_difference = RSpec::Support::FuzzyMatcher.values_match?( @actual_difference, @expected_difference ) end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 209 def match_initial_value? return @match_initial_value unless @match_initial_value.nil? if @expected_initial_value == DEFAULT_VALUE return @match_initial_value = true end @match_initial_value = RSpec::Support::FuzzyMatcher.values_match?( initial_value, @expected_initial_value ) end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 222 def value_has_changed? value_spy.changed? end