Module: Servactory::TestKit::Rspec::Helpers::Fluent

Included in:
Servactory::TestKit::Rspec::Helpers
Defined in:
lib/servactory/test_kit/rspec/helpers/fluent.rb

Overview

Fluent API for mocking Servactory services in RSpec tests.

## Purpose

Provides modern fluent builder interface for configuring service mocks with chainable method calls. This is the recommended API for new tests.

## Usage

Include in RSpec configuration via the main Helpers module:

“‘ruby RSpec.configure do |config|

config.include Servactory::TestKit::Rspec::Helpers, type: :service

end “‘

## Available Methods

  • ‘allow_service(ServiceClass)` - mock `.call` method (returns Result)

  • ‘allow_service!(ServiceClass)` - mock `.call!` method (raises on failure)

## Examples

“‘ruby # Mock successful service call with outputs allow_service(PaymentService)

.succeeds(transaction_id: "txn_123", status: :completed)

# Mock with input matching allow_service(PaymentService)

.with(amount: 100)
.succeeds(transaction_id: "txn_100")

# Mock failure allow_service(PaymentService)

.fails(type: :payment_declined, message: "Card declined")

# Sequential returns allow_service(PaymentService)

.succeeds(status: :pending)
.then_succeeds(status: :completed)

“‘

Instance Method Summary collapse

Instance Method Details

#allow_service(service_class) ⇒ ServiceMockBuilder

Start building a service mock with fluent API for .call method.

When service fails, returns Result with ‘.error` attribute.

Examples:

Success mock

allow_service(PaymentService)
  .succeeds(transaction_id: "txn_123", status: :completed)

Success with input matching

allow_service(PaymentService)
  .with(amount: 100)
  .succeeds(transaction_id: "txn_123")

Failure mock

allow_service(PaymentService)
  .fails(type: :payment_declined, message: "Card declined")

Sequential returns

allow_service(PaymentService)
  .succeeds(status: :pending)
  .then_succeeds(status: :completed)
  .then_fails(type: :timeout, message: "Request timed out")

Parameters:

  • service_class (Class)

    The service class to mock

Returns:



79
80
81
# File 'lib/servactory/test_kit/rspec/helpers/fluent.rb', line 79

def allow_service(service_class)
  Helpers::ServiceMockBuilder.new(service_class, method_type: :call, rspec_context: self)
end

#allow_service!(service_class) ⇒ ServiceMockBuilder

Start building a service mock with fluent API for .call! method.

When service fails, raises exception.

Examples:

Success mock for call!

allow_service!(PaymentService)
  .succeeds(transaction_id: "txn_123", status: :completed)

Failure mock for call! (raises exception)

allow_service!(PaymentService)
  .fails(type: :payment_declined, message: "Insufficient funds")

Sequential returns

allow_service!(RetryService)
  .succeeds(status: :pending)
  .then_fails(type: :timeout, message: "Request timed out")

Parameters:

  • service_class (Class)

    The service class to mock

Returns:



103
104
105
# File 'lib/servactory/test_kit/rspec/helpers/fluent.rb', line 103

def allow_service!(service_class)
  Helpers::ServiceMockBuilder.new(service_class, method_type: :call!, rspec_context: self)
end