Class: Servactory::TestKit::Rspec::Matchers::Result::BeFailureServiceMatcher
- Inherits:
-
Object
- Object
- Servactory::TestKit::Rspec::Matchers::Result::BeFailureServiceMatcher
- Includes:
- RSpec::Matchers::Composable
- Defined in:
- lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb
Overview
RSpec matcher for validating failed service results.
## Purpose
Validates that a service result is a failure with expected error type, message, and metadata. Supports custom failure classes configured via Servactory settings.
## Usage
“‘ruby RSpec.describe MyService, type: :service do
it "fails with validation error" do
result = described_class.call(invalid: true)
expect(result).to be_failure_service
.type(:validation_error)
.("Invalid input provided")
end
it "fails with custom exception and meta" do
result = described_class.call(bad_data: true)
expect(result).to be_failure_service
.with(MyCustomFailure)
.type(:processing_error)
.(field: :data, code: 422)
end
end “‘
## Chain Methods
-
‘.with(Class)` - expected custom failure class
-
‘.type(Symbol)` - expected error type (defaults to `:base`)
-
‘.message(String)` - expected error message
-
‘.meta(Hash)` - expected error metadata
## Validation Steps
-
Checks result is a ‘Servactory::Result` instance
-
Verifies ‘result.success?` returns false
-
Verifies ‘result.failure?` returns true
-
Validates error is a ‘Servactory::Exceptions::Failure`
-
Validates failure class if specified via ‘.with`
-
Validates error type (defaults to ‘:base`)
-
Validates message if specified
-
Validates meta if specified
Instance Method Summary collapse
-
#description ⇒ String
Returns a description of what this matcher validates.
-
#failure_message ⇒ String
Returns detailed failure message explaining what check failed.
-
#failure_message_when_negated ⇒ String
Returns the failure message for negated expectations.
-
#initialize ⇒ BeFailureServiceMatcher
constructor
Creates a new failure matcher with empty expectations.
-
#matches?(result) ⇒ Boolean
Performs the match against the actual service result.
-
#message(expected_message) ⇒ self
Specifies the expected error message.
-
#meta(expected_meta) ⇒ self
Specifies the expected error metadata.
-
#supports_block_expectations? ⇒ Boolean
Indicates this matcher does not support block expectations.
-
#type(expected_type) ⇒ self
Specifies the expected error type.
-
#with(failure_class) ⇒ self
Specifies the expected custom failure class.
Constructor Details
#initialize ⇒ BeFailureServiceMatcher
Creates a new failure matcher with empty expectations.
62 63 64 65 66 67 68 69 70 |
# File 'lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb', line 62 def initialize @expected_failure_class = nil @expected_type = nil @expected_message = nil @expected_meta = nil @type_defined = false @message_defined = false @meta_defined = false end |
Instance Method Details
#description ⇒ String
Returns a description of what this matcher validates.
104 105 106 |
# File 'lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb', line 104 def description "service failure" end |
#failure_message ⇒ String
Returns detailed failure message explaining what check failed.
Checks in order: result type, failure status, error class, failure class, error type, message, and meta.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb', line 114 def # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity unless result.is_a?(Servactory::Result) return <<~MESSAGE Incorrect service result: expected Servactory::Result got #{result.class.name} MESSAGE end if result.success? return <<~MESSAGE Incorrect service result: expected failure got success MESSAGE end unless result.error.is_a?(Servactory::Exceptions::Failure) return <<~MESSAGE Incorrect error object: expected Servactory::Exceptions::Failure got #{result.error.class.name} MESSAGE end if expected_failure_class && !result.error.is_a?(expected_failure_class) return <<~MESSAGE Incorrect instance error: expected #{expected_failure_class} got #{result.error.class.name} MESSAGE end expected_type_value = @type_defined ? expected_type : :base if result.error.type != expected_type_value return <<~MESSAGE Incorrect error type: expected #{expected_type_value.inspect} got #{result.error.type.inspect} MESSAGE end if @message_defined && result.error. != return <<~MESSAGE Incorrect error message: expected #{.inspect} got #{result.error..inspect} MESSAGE end if @meta_defined && result.error. != return <<~MESSAGE Incorrect error meta: expected #{.inspect} got #{result.error..inspect} MESSAGE end <<~MESSAGE Unexpected case when using `be_failure_service`. Exception: #{result.error.inspect} Type: #{result.error.type.inspect} Message: #{result.error..inspect} Meta: #{result.error..inspect} Please try to build an example based on the documentation. Or report your problem to us: https://github.com/servactory/servactory/issues MESSAGE end |
#failure_message_when_negated ⇒ String
Returns the failure message for negated expectations.
197 198 199 |
# File 'lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb', line 197 def "Expected result not to be a failed service" end |
#matches?(result) ⇒ Boolean
Performs the match against the actual service result.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb', line 83 def matches?(result) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity @result = result failure_class = expected_failure_class || Servactory::Exceptions::Failure type = @type_defined ? expected_type : :base matched = result.is_a?(Servactory::Result) matched &&= !result.success? matched &&= result.failure? matched &&= result.error.is_a?(Servactory::Exceptions::Failure) matched &&= result.error.is_a?(failure_class) matched &&= result.error.type == type matched &&= result.error. == if @message_defined matched &&= result.error. == if @meta_defined matched end |
#message(expected_message) ⇒ self
Specifies the expected error message.
229 230 231 232 233 |
# File 'lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb', line 229 def () @expected_message = @message_defined = true self end |
#meta(expected_meta) ⇒ self
Specifies the expected error metadata.
239 240 241 242 243 |
# File 'lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb', line 239 def () @expected_meta = @meta_defined = true self end |
#supports_block_expectations? ⇒ Boolean
Indicates this matcher does not support block expectations.
75 76 77 |
# File 'lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb', line 75 def supports_block_expectations? false end |
#type(expected_type) ⇒ self
Specifies the expected error type.
219 220 221 222 223 |
# File 'lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb', line 219 def type(expected_type) @expected_type = expected_type @type_defined = true self end |
#with(failure_class) ⇒ self
Specifies the expected custom failure class.
Use when service is configured with a custom failure_class.
210 211 212 213 |
# File 'lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb', line 210 def with(failure_class) @expected_failure_class = failure_class self end |