Class: Servactory::TestKit::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/servactory/test_kit/result.rb

Overview

Factory for creating mock Servactory result objects.

## Purpose

Provides factory methods for creating success and failure result objects in tests without actually calling a service. Used internally by the service mocking helpers to generate return values.

## Usage

“‘ruby # Create success result with outputs result = Servactory::TestKit::Result.as_success(

service_class: MyService,
user: user_object,
status: :active

)

# Create failure result with exception result = Servactory::TestKit::Result.as_failure(

service_class: MyService,
exception: Servactory::Exceptions::Failure.new(message: "Error")

) “‘

## Service Class Resolution

When ‘service_class` is provided, uses that service’s configured ‘result_class` for proper result type. Otherwise, uses the default `Servactory::Result` class.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Result

Initializes result with attribute accessors.

Parameters:

  • attributes (Hash) (defaults to: {})

    Output name-value pairs



73
74
75
76
77
# File 'lib/servactory/test_kit/result.rb', line 73

def initialize(attributes = {})
  attributes.each_pair do |name, value|
    servactory_service_warehouse.assign_output(name, value)
  end
end

Class Method Details

.as_failure(**attributes) ⇒ Servactory::Result

Creates a failed mock result with given exception.

Parameters:

  • attributes (Hash)

    Output attributes, :exception, and optional :service_class

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/servactory/test_kit/result.rb', line 56

def self.as_failure(**attributes)
  service_class = attributes.delete(:service_class) || self
  exception = attributes.delete(:exception)

  context = new(attributes)

  if service_class == Servactory::TestKit::Result
    Servactory::Result.failure_for(context:, exception:)
  else
    service_class.config.result_class.failure_for(context:, exception:)
  end
end

.as_success(**attributes) ⇒ Servactory::Result

Creates a successful mock result with given outputs.

Parameters:

  • attributes (Hash)

    Output attributes and optional :service_class

Returns:



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/servactory/test_kit/result.rb', line 40

def self.as_success(**attributes)
  service_class = attributes.delete(:service_class) || self

  context = new(attributes)

  if service_class == Servactory::TestKit::Result
    Servactory::Result.success_for(context:)
  else
    service_class.config.result_class.success_for(context:)
  end
end