Class: Servactory::TestKit::Rspec::Matchers::Result::BeSuccessServiceMatcher

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers::Composable
Defined in:
lib/servactory/test_kit/rspec/matchers/result/be_success_service_matcher.rb

Overview

RSpec matcher for validating successful service results.

## Purpose

Validates that a service result is successful and optionally contains expected output values. Used in integration tests to verify service execution completed without failure.

## Usage

“‘ruby RSpec.describe MyService, type: :service do

it "succeeds with expected outputs" do
  result = described_class.call(user_id: 123)

  expect(result).to be_success_service
    .with_output(:user_name, "John")
    .with_output(:status, "active")
end

it "succeeds with multiple outputs" do
  result = described_class.call(data: input_data)

  expect(result).to be_success_service
    .with_outputs(processed: true, count: 5)
end

end “‘

## Chain Methods

  • ‘.with_output(key, value)` - validates single output value

  • ‘.with_outputs(hash)` - validates multiple output values at once

## Validation Steps

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

  2. Verifies ‘result.success?` returns true

  3. Verifies ‘result.failure?` returns false

  4. Validates all expected outputs match actual values

Instance Method Summary collapse

Constructor Details

#initializeBeSuccessServiceMatcher

Creates a new success matcher with empty output expectations.



54
55
56
# File 'lib/servactory/test_kit/rspec/matchers/result/be_success_service_matcher.rb', line 54

def initialize
  @expected_outputs = {}
end

Instance Method Details

#descriptionString

Returns a description of what this matcher validates.

Returns:

  • (String)

    Human-readable matcher description



86
87
88
# File 'lib/servactory/test_kit/rspec/matchers/result/be_success_service_matcher.rb', line 86

def description
  "service success"
end

#failure_messageString

Returns detailed failure message explaining what check failed.

Checks in order: result type, success status, output existence, output values.

Returns:

  • (String)

    Detailed failure message



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
# File 'lib/servactory/test_kit/rspec/matchers/result/be_success_service_matcher.rb', line 95

def failure_message # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
  unless result.is_a?(Servactory::Result)
    return <<~MESSAGE
      Incorrect service result:

        expected Servactory::Result
             got #{result.class.name}
    MESSAGE
  end

  if result.failure?
    return <<~MESSAGE
      Incorrect service result:

        expected success
             got failure
    MESSAGE
  end

  message = expected_outputs.each do |key, value|
    unless result.respond_to?(key)
      break <<~MESSAGE
        Non-existent value key in result:

        expected #{result.inspect}
             got #{key}
      MESSAGE
    end

    received_value = result.public_send(key)
    next if received_value == value

    break <<~MESSAGE
      Incorrect result value for #{key}:

        expected #{value.inspect}
             got #{received_value.inspect}
    MESSAGE
  end

  return message if message.present? && message.is_a?(String)

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

    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



150
151
152
# File 'lib/servactory/test_kit/rspec/matchers/result/be_success_service_matcher.rb', line 150

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

#matches?(result) ⇒ Boolean

Performs the match against the actual service result.

Parameters:

Returns:

  • (Boolean)

    True if result is successful with matching outputs



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/servactory/test_kit/rspec/matchers/result/be_success_service_matcher.rb', line 69

def matches?(result)
  @result = result

  matched = result.is_a?(Servactory::Result)
  matched &&= result.success?
  matched &&= !result.failure?

  matched &&= expected_outputs.all? do |key, value|
    result.respond_to?(key) && result.public_send(key) == value
  end

  matched
end

#supports_block_expectations?Boolean

Indicates this matcher does not support block expectations.

Returns:

  • (Boolean)

    Always false



61
62
63
# File 'lib/servactory/test_kit/rspec/matchers/result/be_success_service_matcher.rb', line 61

def supports_block_expectations?
  false
end

#with_output(key, value) ⇒ self

Specifies an expected output value on the result.

Parameters:

  • key (Symbol)

    Output attribute name

  • value (Object)

    Expected value

Returns:

  • (self)

    For method chaining



162
163
164
165
# File 'lib/servactory/test_kit/rspec/matchers/result/be_success_service_matcher.rb', line 162

def with_output(key, value)
  expected_outputs[key] = value
  self
end

#with_outputs(attributes) ⇒ self

Specifies multiple expected output values at once.

Parameters:

  • attributes (Hash{Symbol => Object})

    Expected output key-value pairs

Returns:

  • (self)

    For method chaining



171
172
173
174
# File 'lib/servactory/test_kit/rspec/matchers/result/be_success_service_matcher.rb', line 171

def with_outputs(attributes)
  attributes.each { |key, value| expected_outputs[key] = value }
  self
end