Module: Servactory::TestKit::Rspec::Helpers::Legacy

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

Overview

Backward-compatible API for mocking Servactory services in RSpec tests.

## Purpose

Provides legacy helper methods for mocking Servactory service calls. These methods are maintained for backward compatibility with existing tests. For new tests, consider using the fluent API via Fluent module.

## 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_as_success!` - mock `.call!` method for success

  • ‘allow_service_as_success` - mock `.call` method for success

  • ‘allow_service_as_failure!` - mock `.call!` method for failure

  • ‘allow_service_as_failure` - mock `.call` method for failure

## Examples

“‘ruby # Mock call method (returns Result) allow_service_as_success(PaymentService) do

{ transaction_id: "txn_123" }

end

# Mock call! method (raises on failure) allow_service_as_success!(PaymentService, with: { amount: 100 }) do

{ transaction_id: "txn_123" }

end

# Mock failure allow_service_as_failure(PaymentService) do

{
  exception: ApplicationService::Exceptions::Failure.new(
    type: :base,
    message: "Failed"
  )
}

end “‘

Instance Method Summary collapse

Instance Method Details

#allow_service_as_failure(service_class, with: nil) { ... } ⇒ void

This method returns an undefined value.

Mock the ‘call` method to return a failure result.

Examples:

Failure mock with exception in hash

allow_service_as_failure(PaymentService) do
  {
    exception: ApplicationService::Exceptions::Failure.new(
      type: :base,
      message: "Failed"
    )
  }
end

Parameters:

  • service_class (Class)

    The service class to mock

  • with (Hash, nil) (defaults to: nil)

    Expected arguments matcher

Yields:

  • Block returning an exception or Hash with ‘:exception` key



131
132
133
# File 'lib/servactory/test_kit/rspec/helpers/legacy.rb', line 131

def allow_service_as_failure(service_class, with: nil, &block)
  allow_service_legacy(service_class, :as_failure, with:, &block)
end

#allow_service_as_failure!(service_class, with: nil) { ... } ⇒ void

This method returns an undefined value.

Mock the ‘call!` method to raise a failure exception.

Examples:

Failure mock

allow_service_as_failure!(PaymentService) do
  ApplicationService::Exceptions::Failure.new(
    type: :payment_declined,
    message: "Card declined"
  )
end

Parameters:

  • service_class (Class)

    The service class to mock

  • with (Hash, nil) (defaults to: nil)

    Expected arguments matcher

Yields:

  • Block returning an exception or Hash with ‘:exception` key



110
111
112
# File 'lib/servactory/test_kit/rspec/helpers/legacy.rb', line 110

def allow_service_as_failure!(service_class, with: nil, &block)
  allow_service_legacy!(service_class, :as_failure, with:, &block)
end

#allow_service_as_success(service_class, with: nil) { ... } ⇒ void

This method returns an undefined value.

Mock the ‘call` method to return a successful result.

Examples:

Basic success mock

allow_service_as_success(PaymentService) do
  { transaction_id: "txn_123" }
end

Parameters:

  • service_class (Class)

    The service class to mock

  • with (Hash, nil) (defaults to: nil)

    Expected arguments matcher

Yields:

  • Block returning Hash of output attributes



91
92
93
# File 'lib/servactory/test_kit/rspec/helpers/legacy.rb', line 91

def allow_service_as_success(service_class, with: nil, &block)
  allow_service_legacy(service_class, :as_success, with:, &block)
end

#allow_service_as_success!(service_class, with: nil) { ... } ⇒ void

This method returns an undefined value.

Mock the ‘call!` method to return a successful result.

Examples:

Basic success mock

allow_service_as_success!(PaymentService) do
  { transaction_id: "txn_123" }
end

With argument matcher

allow_service_as_success!(PaymentService, with: { amount: 100 }) do
  { transaction_id: "txn_123" }
end

Parameters:

  • service_class (Class)

    The service class to mock

  • with (Hash, nil) (defaults to: nil)

    Expected arguments matcher

Yields:

  • Block returning Hash of output attributes



75
76
77
# File 'lib/servactory/test_kit/rspec/helpers/legacy.rb', line 75

def allow_service_as_success!(service_class, with: nil, &block)
  allow_service_legacy!(service_class, :as_success, with:, &block)
end