class RSpec::SleepingKingStudios::Matchers::Core::HaveWriterMatcher

Matcher for testing whether an object has a specific property writer, e.g. responds to :property= and updates the state.

@since 1.0.0

Public Class Methods

new(expected, allow_private: false) click to toggle source

@param [String, Symbol] expected the property to check for on the actual

object
# File lib/rspec/sleeping_king_studios/matchers/core/have_writer_matcher.rb, line 17
def initialize expected, allow_private: false
  @expected      = expected.to_s.gsub(/=$/,'').intern
  @allow_private = allow_private
end

Public Instance Methods

allow_private?() click to toggle source

@return [Boolean] True if the matcher matches private reader methods,

otherwise false.
# File lib/rspec/sleeping_king_studios/matchers/core/have_writer_matcher.rb, line 24
def allow_private?
  !!@allow_private
end
description() click to toggle source

Generates a description of the matcher expectation.

@return [String] The matcher description.

# File lib/rspec/sleeping_king_studios/matchers/core/have_writer_matcher.rb, line 31
def description
  "have writer :#{@expected}"
end
failure_message() click to toggle source

@see BaseMatcher#failure_message

# File lib/rspec/sleeping_king_studios/matchers/core/have_writer_matcher.rb, line 51
def failure_message
  "expected #{@actual.inspect} to respond to :#{@expected}="\
  ", but did not respond to :#{@expected}="
end
failure_message_when_negated() click to toggle source

@see BaseMatcher#failure_message_when_negated

# File lib/rspec/sleeping_king_studios/matchers/core/have_writer_matcher.rb, line 57
def failure_message_when_negated
  "expected #{@actual.inspect} not to respond to :#{@expected}="\
  ", but responded to :#{@expected}="
end
matches?(actual) click to toggle source

Checks if the object responds to :expected=. Additionally, if a value expectation is set, assigns the value via :expected= and compares the subsequent value to the specified value using :expected or the block provided to with.

@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
# File lib/rspec/sleeping_king_studios/matchers/core/have_writer_matcher.rb, line 44
def matches? actual
  super

  responds_to_writer?(:allow_private => allow_private?)
end