Class: Servactory::TestKit::Rspec::Matchers::Result::BeSuccessServiceMatcher
- Inherits:
-
Object
- Object
- Servactory::TestKit::Rspec::Matchers::Result::BeSuccessServiceMatcher
- 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
-
Checks result is a ‘Servactory::Result` instance
-
Verifies ‘result.success?` returns true
-
Verifies ‘result.failure?` returns false
-
Validates all expected outputs match actual values
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 ⇒ BeSuccessServiceMatcher
constructor
Creates a new success matcher with empty output expectations.
-
#matches?(result) ⇒ Boolean
Performs the match against the actual service result.
-
#supports_block_expectations? ⇒ Boolean
Indicates this matcher does not support block expectations.
-
#with_output(key, value) ⇒ self
Specifies an expected output value on the result.
-
#with_outputs(attributes) ⇒ self
Specifies multiple expected output values at once.
Constructor Details
#initialize ⇒ BeSuccessServiceMatcher
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
#description ⇒ String
Returns a description of what this matcher validates.
86 87 88 |
# File 'lib/servactory/test_kit/rspec/matchers/result/be_success_service_matcher.rb', line 86 def description "service success" end |
#failure_message ⇒ String
Returns detailed failure message explaining what check failed.
Checks in order: result type, success status, output existence, output values.
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 # 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 = 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 if .present? && .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_negated ⇒ String
Returns the failure message for negated expectations.
150 151 152 |
# File 'lib/servactory/test_kit/rspec/matchers/result/be_success_service_matcher.rb', line 150 def "Expected result not to be a successful service" end |
#matches?(result) ⇒ Boolean
Performs the match against the actual service result.
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.
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.
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.
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 |