Class: RSpec::SleepingKingStudios::Matchers::ActiveModel::HaveErrorsMatcher
- Inherits:
-
BaseMatcher
- Object
- BaseMatcher
- RSpec::SleepingKingStudios::Matchers::ActiveModel::HaveErrorsMatcher
- Includes:
- HaveErrors
- Defined in:
- lib/rspec/sleeping_king_studios/matchers/active_model/have_errors_matcher.rb
Overview
Matcher for testing ActiveModel object validations.
Constant Summary
Constants included from Description
Description::DEFAULT_EXPECTED_ITEMS
Instance Attribute Summary
Attributes inherited from BaseMatcher
Instance Method Summary collapse
- #description ⇒ Object
-
#does_not_match?(actual) ⇒ Boolean
Checks if the object can be validated, whether the object is valid, and checks the errors on the object against the expected errors and messages from #on and #with_message, if any.
-
#failure_message ⇒ Object
Message for when the object does not match, but was expected to.
-
#failure_message_when_negated ⇒ Object
Message for when the object matches, but was expected not to.
-
#initialize ⇒ HaveErrorsMatcher
constructor
A new instance of HaveErrorsMatcher.
-
#matches?(actual) ⇒ Boolean
Checks if the object can be validated, whether the object is valid, and checks the errors on the object against the expected errors and messages from #on and #with_message, if any.
-
#on(attribute) ⇒ HaveErrorsMatcher
Adds an error expectation.
-
#with_message(message) ⇒ HaveErrorsMatcher
Adds a message expectation for the most recently added error attribute.
-
#with_messages(*messages) ⇒ Object
(also: #with)
Adds a set of message expectations for the most recently added error attribute.
Constructor Details
#initialize ⇒ HaveErrorsMatcher
Returns a new instance of HaveErrorsMatcher.
15 16 17 18 19 20 21 |
# File 'lib/rspec/sleeping_king_studios/matchers/active_model/have_errors_matcher.rb', line 15 def initialize super # The error and message expectations are set up through #on and # #with_message. @error_expectations = [] end |
Instance Method Details
#description ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rspec/sleeping_king_studios/matchers/active_model/have_errors_matcher.rb', line 24 def description = 'have errors' = @error_expectations.select(&:expected).map do |expectation| = ":#{expectation.attribute}" = expectation..select(&:expected).map do || %{"#{.}"} end # map unless .empty? tools = ::SleepingKingStudios::Tools::ArrayTools << " with message#{.count == 1 ? '' : 's'} "\ "#{tools.humanize_list }" end # unless end # each << " on #{.join(", and on ")}" unless .empty? end |
#does_not_match?(actual) ⇒ Boolean
Checks if the object can be validated, whether the object is valid, and checks the errors on the object against the expected errors and messages from #on and #with_message, if any.
60 61 62 63 64 65 66 67 68 |
# File 'lib/rspec/sleeping_king_studios/matchers/active_model/have_errors_matcher.rb', line 60 def does_not_match? actual super @negative_expectation = true return false unless @validates = actual.respond_to?(:valid?) !matches?(actual) end |
#failure_message ⇒ Object
Message for when the object does not match, but was expected to. Make sure to always call #matches? first to set up the matcher state.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/rspec/sleeping_king_studios/matchers/active_model/have_errors_matcher.rb', line 143 def # Failure cases: # * object is not a model ("to respond to valid") # * expected one or more errors, but received none ("to have errors") # * expected one or more messages on :attribute, but received none or a # subset ("to have errors on") if !@validates "expected #{@actual.inspect} to respond to :valid?" elsif expected_errors.empty? "expected #{@actual.inspect} to have errors" else "expected #{@actual.inspect} to have errors#{}#{}" end # if-elsif-else end |
#failure_message_when_negated ⇒ Object
Message for when the object matches, but was expected not to. Make sure to always call #matches? first to set up the matcher state.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/rspec/sleeping_king_studios/matchers/active_model/have_errors_matcher.rb', line 160 def # Failure cases: # * object is not a model ("to respond to valid") # * expected one or more errors, received one or more ("not to have # errors") # * expected one or more messages on attribute, received one or more # ("not to have errors on") # * expected specific messages on attribute, received all ("not to have # errors on") if !@validates "expected #{@actual.inspect} to respond to :valid?" elsif expected_errors.empty? return "expected #{@actual.inspect} not to have errors#{}" else return "expected #{@actual.inspect} not to have errors#{}#{}" end # if-else end |
#matches?(actual) ⇒ Boolean
Checks if the object can be validated, whether the object is valid, and checks the errors on the object against the expected errors and messages from #on and #with_message, if any.
81 82 83 84 85 86 87 |
# File 'lib/rspec/sleeping_king_studios/matchers/active_model/have_errors_matcher.rb', line 81 def matches? actual super return false unless @validates = actual.respond_to?(:valid?) !@actual.valid? && attributes_have_errors? end |
#on(attribute) ⇒ HaveErrorsMatcher
Adds an error expectation. If the actual object does not have an error on the specified attribute, #matches? will return false.
98 99 100 101 102 |
# File 'lib/rspec/sleeping_king_studios/matchers/active_model/have_errors_matcher.rb', line 98 def on attribute @error_expectations << ErrorExpectation.new(attribute) self end |
#with_message(message) ⇒ HaveErrorsMatcher
Adds a message expectation for the most recently added error attribute. If the actual object does not have an error on the that attribute with the specified message, #matches? will return false.
120 121 122 123 124 125 126 127 |
# File 'lib/rspec/sleeping_king_studios/matchers/active_model/have_errors_matcher.rb', line 120 def raise ArgumentError.new "no attribute specified for error message" if @error_expectations.empty? @error_expectations.last. << MessageExpectation.new() self end |
#with_messages(*messages) ⇒ Object Also known as: with
Adds a set of message expectations for the most recently added error attribute.
135 136 137 138 139 |
# File 'lib/rspec/sleeping_king_studios/matchers/active_model/have_errors_matcher.rb', line 135 def * .each do || self.(); end self end |