class RSpec::SleepingKingStudios::Matchers::Core::HaveReaderMatcher
Matcher for testing whether an object has a specific property reader, e.g. responds to :property.
@since 1.0.0
Public Class Methods
@param [String, Symbol] expected The property to check for on the actual
object.
@param [Boolean] allow_private If true, then the matcher will match a
protected or private reader method. Defaults to false.
# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 19 def initialize expected, allow_private: false @expected = expected.intern @allow_private = allow_private end
Public Instance Methods
@return [Boolean] True if the matcher matches private reader methods,
otherwise false.
# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 26 def allow_private? !!@allow_private end
(see BaseMatcher#description
)
# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 31 def description value_message = value_to_string "have reader :#{@expected}#{@value_set ? " with value #{value_message}" : ''}" end
(see BaseMatcher#does_not_match?
)
RSpec::SleepingKingStudios::Matchers::BaseMatcher#does_not_match?
# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 37 def does_not_match? actual super matches_reader?(:none?) end
(see BaseMatcher#failure_message
)
# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 71 def failure_message message = "expected #{@actual.inspect} to respond to :#{@expected}" message << " and return #{value_to_string}" if @value_set if !@matches_reader message << ", but did not respond to :#{@expected}" elsif !@matches_reader_value message << ", but returned #{@actual.send(@expected).inspect}" end # if message end
(see BaseMatcher#failure_message_when_negated
)
# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 85 def failure_message_when_negated message = "expected #{@actual.inspect} not to respond to :#{@expected}" message << " and return #{value_to_string}" if @value_set errors = [] errors << "responded to :#{@expected}" if @matches_reader errors << "returned #{@actual.send(@expected).inspect}" if @matches_reader_value message << ", but #{errors.join(" and ")}" message end
Checks if the object responds to expected. Additionally, if a value expectation is set, compares the value of expected to the specified value.
@param [Object] actual The object to check.
@return [Boolean] true If the object responds to expected and matches
the value expectation (if any); otherwise false.
RSpec::SleepingKingStudios::Matchers::BaseMatcher#matches?
# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 51 def matches? actual super matches_reader?(:all?) end
Sets a value expectation. The matcher will compare the value from property with the specified value.
@param [Object] value The value to compare.
@return [HaveReaderMatcher] self
# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 63 def with value @value = value @value_set = true self end
Private Instance Methods
# File lib/rspec/sleeping_king_studios/matchers/core/have_reader_matcher.rb, line 99 def matches_reader? filter [ responds_to_reader?(:allow_private => allow_private?), matches_reader_value?(:allow_private => allow_private?) ].send(filter) { |bool| bool } end