Class: Servactory::TestKit::Result
- Inherits:
-
Object
- Object
- Servactory::TestKit::Result
- 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
-
.as_failure(**attributes) ⇒ Servactory::Result
Creates a failed mock result with given exception.
-
.as_success(**attributes) ⇒ Servactory::Result
Creates a successful mock result with given outputs.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Result
constructor
Initializes result with attribute accessors.
Constructor Details
#initialize(attributes = {}) ⇒ Result
Initializes result with attribute accessors.
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.
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.
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 |