Class: Cuprum::RSpec::BeAResultMatcher
- Inherits:
-
Object
- Object
- Cuprum::RSpec::BeAResultMatcher
- Defined in:
- lib/cuprum/rspec/be_a_result_matcher.rb
Overview
Asserts the actual object is a result object with the specified properties.
If initialized with a class, the matcher will assert that the actual object is an instance of that class. This can be useful for asserting that the result is an instance of a result subclass. If no class is given, the matcher asserts that the result is an object responding to #to_cuprum_result.
The matcher also defines fluent methods for asserting on the result’s properties:
-
The #with_value method asserts that the result has the specified value. Also aliased as #and_value.
-
The #with_error method asserts that the result has the specified error. Also aliased as #and_error.
-
The #with_status method asserts that the result has the specified status. Also aliased as #and_status.
Generally speaking, you should use the #be_a_result, #be_a_passing_result, and #be_a_failing_result macros, rather than instantiating a BeAResultMatcher directly.
Instance Attribute Summary collapse
-
#expected_class ⇒ Class
readonly
The expected class of result.
Instance Method Summary collapse
-
#description ⇒ String
A short description of the matcher and expected properties.
-
#does_not_match?(actual) ⇒ Boolean
Checks that the given actual object is not a Cuprum result.
-
#failure_message ⇒ String
A summary message describing a failed expectation.
-
#failure_message_when_negated ⇒ String
A summary message describing a failed negated expectation.
-
#initialize(expected_class = nil) ⇒ BeAResultMatcher
constructor
A new instance of BeAResultMatcher.
-
#matches?(actual) ⇒ Boolean
Checks that the given actual object is a Cuprum result or compatible object and has the specified properties.
-
#with_error(error) ⇒ BeAResultMatcher
(also: #and_error)
Sets an error expectation on the matcher.
-
#with_status(status) ⇒ BeAResultMatcher
(also: #and_status)
Sets a status expectation on the matcher.
-
#with_value(value) ⇒ BeAResultMatcher
(also: #and_value)
Sets a value expectation on the matcher.
Constructor Details
#initialize(expected_class = nil) ⇒ BeAResultMatcher
Returns a new instance of BeAResultMatcher.
78 79 80 81 82 |
# File 'lib/cuprum/rspec/be_a_result_matcher.rb', line 78 def initialize(expected_class = nil) @expected_class = expected_class @expected_error = DEFAULT_VALUE @expected_value = DEFAULT_VALUE end |
Instance Attribute Details
#expected_class ⇒ Class (readonly)
Returns the expected class of result.
85 86 87 |
# File 'lib/cuprum/rspec/be_a_result_matcher.rb', line 85 def expected_class @expected_class end |
Instance Method Details
#description ⇒ String
Returns a short description of the matcher and expected properties.
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/cuprum/rspec/be_a_result_matcher.rb', line 89 def description = if expected_class "be an instance of #{expected_class}" else 'be a Cuprum result' end return unless expected_properties? "#{} #{properties_description}" end |
#does_not_match?(actual) ⇒ Boolean
Checks that the given actual object is not a Cuprum result.
107 108 109 110 111 112 113 |
# File 'lib/cuprum/rspec/be_a_result_matcher.rb', line 107 def does_not_match?(actual) @actual = actual raise ArgumentError, negated_matcher_warning if expected_properties? !actual_is_result? end |
#failure_message ⇒ String
Returns a summary message describing a failed expectation.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/cuprum/rspec/be_a_result_matcher.rb', line 116 def # rubocop:disable Metrics/MethodLength = "expected #{actual.inspect} to #{description}" if !actual_is_result? && expected_class "#{}, but the object is not an instance of #{expected_class}" elsif !actual_is_result? "#{}, but the object is not a result" elsif actual_is_uncalled_operation? "#{}, but the object is an uncalled operation" elsif !properties_match? + else # :nocov: # :nocov: end end |
#failure_message_when_negated ⇒ String
Returns a summary message describing a failed negated expectation.
136 137 138 |
# File 'lib/cuprum/rspec/be_a_result_matcher.rb', line 136 def "expected #{actual.inspect} not to #{description}" end |
#matches?(actual) ⇒ Boolean
Checks that the given actual object is a Cuprum result or compatible object and has the specified properties.
147 148 149 150 151 152 |
# File 'lib/cuprum/rspec/be_a_result_matcher.rb', line 147 def matches?(actual) @actual = actual @non_matching_properties = nil actual_is_result? && !actual_is_uncalled_operation? && properties_match? end |
#with_error(error) ⇒ BeAResultMatcher Also known as: and_error
Sets an error expectation on the matcher. Calls to #matches? will fail unless the actual object has the specified error.
160 161 162 163 164 |
# File 'lib/cuprum/rspec/be_a_result_matcher.rb', line 160 def with_error(error) @expected_error = error self end |
#with_status(status) ⇒ BeAResultMatcher Also known as: and_status
Sets a status expectation on the matcher. Calls to #matches? will fail unless the actual object has the specified status.
173 174 175 176 177 |
# File 'lib/cuprum/rspec/be_a_result_matcher.rb', line 173 def with_status(status) @expected_status = status self end |
#with_value(value) ⇒ BeAResultMatcher Also known as: and_value
Sets a value expectation on the matcher. Calls to #matches? will fail unless the actual object has the specified value.
186 187 188 189 190 |
# File 'lib/cuprum/rspec/be_a_result_matcher.rb', line 186 def with_value(value) @expected_value = value self end |