Class: Servactory::TestKit::Rspec::Matchers::Result::BeFailureServiceMatcher

Inherits:
Object
  • Object
show all
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)
    .message("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)
    .meta(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

  1. Checks result is a ‘Servactory::Result` instance

  2. Verifies ‘result.success?` returns false

  3. Verifies ‘result.failure?` returns true

  4. Validates error is a ‘Servactory::Exceptions::Failure`

  5. Validates failure class if specified via ‘.with`

  6. Validates error type (defaults to ‘:base`)

  7. Validates message if specified

  8. Validates meta if specified

Instance Method Summary collapse

Constructor Details

#initializeBeFailureServiceMatcher

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

#descriptionString

Returns a description of what this matcher validates.

Returns:

  • (String)

    Human-readable matcher description



104
105
106
# File 'lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb', line 104

def description
  "service failure"
end

#failure_messageString

Returns detailed failure message explaining what check failed.

Checks in order: result type, failure status, error class, failure class, error type, message, and meta.

Returns:

  • (String)

    Detailed failure message with expected vs actual



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 failure_message # 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.message != expected_message
    return <<~MESSAGE
      Incorrect error message:

        expected #{expected_message.inspect}
             got #{result.error.message.inspect}
    MESSAGE
  end

  if @meta_defined && result.error.meta != expected_meta
    return <<~MESSAGE
      Incorrect error meta:

        expected #{expected_meta.inspect}
             got #{result.error.meta.inspect}
    MESSAGE
  end

  <<~MESSAGE
    Unexpected case when using `be_failure_service`.

    Exception:  #{result.error.inspect}
    Type:       #{result.error.type.inspect}
    Message:    #{result.error.message.inspect}
    Meta:       #{result.error.meta.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_negatedString

Returns the failure message for negated expectations.

Returns:

  • (String)

    Negated failure message



197
198
199
# File 'lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb', line 197

def failure_message_when_negated
  "Expected result not to be a failed service"
end

#matches?(result) ⇒ Boolean

Performs the match against the actual service result.

Parameters:

Returns:

  • (Boolean)

    True if result is failure with matching error attributes



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.message == expected_message if @message_defined
  matched &&= result.error.meta == expected_meta if @meta_defined

  matched
end

#message(expected_message) ⇒ self

Specifies the expected error message.

Parameters:

  • expected_message (String)

    Expected error message text

Returns:

  • (self)

    For method chaining



229
230
231
232
233
# File 'lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb', line 229

def message(expected_message)
  @expected_message = expected_message
  @message_defined = true
  self
end

#meta(expected_meta) ⇒ self

Specifies the expected error metadata.

Parameters:

  • expected_meta (Hash)

    Expected meta hash

Returns:

  • (self)

    For method chaining



239
240
241
242
243
# File 'lib/servactory/test_kit/rspec/matchers/result/be_failure_service_matcher.rb', line 239

def meta(expected_meta)
  @expected_meta = expected_meta
  @meta_defined = true
  self
end

#supports_block_expectations?Boolean

Indicates this matcher does not support block expectations.

Returns:

  • (Boolean)

    Always false



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.

Parameters:

  • expected_type (Symbol)

    Expected type (defaults to :base if not set)

Returns:

  • (self)

    For method chaining



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.

Parameters:

  • failure_class (Class)

    Expected failure exception class

Returns:

  • (self)

    For method chaining



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