class RSpec::SleepingKingStudios::Matchers::Core::HaveChangedMatcher
Matcher for testing the change in a value.
@since 2.4.0
Public Class Methods
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 15 def initialize super @expected_initial_value = DEFAULT_VALUE @expected_current_value = DEFAULT_VALUE end
Public Instance Methods
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.
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 30 def by(difference) @expected_difference = difference self end
(see BaseMatcher#description
)
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 37 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#does_not_match?
)
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 56 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
(see BaseMatcher#failure_message
)
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 77 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
(see BaseMatcher#failure_message_when_negated
)
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 108 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
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.
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 130 def from(value) @expected_initial_value = value self 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?
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 144 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
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.
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 163 def to(value) @expected_current_value = value self end
Private Instance Methods
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 173 def current_value value_spy.current_value end
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 177 def initial_value value_spy.initial_value end
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 181 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
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 194 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
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 207 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
# File lib/rspec/sleeping_king_studios/matchers/core/have_changed_matcher.rb, line 220 def value_has_changed? value_spy.changed? end