Class: Servactory::TestKit::Rspec::Helpers::ServiceMockConfig
- Inherits:
-
Object
- Object
- Servactory::TestKit::Rspec::Helpers::ServiceMockConfig
- Defined in:
- lib/servactory/test_kit/rspec/helpers/service_mock_config.rb
Overview
Configuration object for a single service mock.
## Purpose
Holds all configuration for mocking a single service call, including result type (success/failure), method type (call/call!), outputs, exceptions, and argument matchers. Used by ServiceMockBuilder and MockExecutor.
## Usage
Usually created internally by ServiceMockBuilder:
“‘ruby config = ServiceMockConfig.new(service_class: MyService) config.result_type = :success config.outputs = { user: user } config.method_type = :call “`
## Attributes
-
‘service_class` - The service class being mocked
-
‘result_type` - :success or :failure
-
‘method_type` - :call or :call!
-
‘outputs` - Hash of output values
-
‘exception` - Exception for failure mocks
-
‘argument_matcher` - RSpec argument matcher or Hash
Instance Attribute Summary collapse
-
#argument_matcher ⇒ Object
Returns the value of attribute argument_matcher.
-
#exception ⇒ Object
Returns the value of attribute exception.
-
#method_type ⇒ Object
Returns the value of attribute method_type.
-
#outputs ⇒ Object
Returns the value of attribute outputs.
-
#result_type ⇒ Object
Returns the value of attribute result_type.
-
#service_class ⇒ Object
Returns the value of attribute service_class.
Instance Method Summary collapse
-
#bang_method? ⇒ Boolean
Checks if this mocks the .call! method.
-
#build_argument_matcher(rspec_context) ⇒ Object
Builds RSpec argument matcher from config.
-
#build_result ⇒ Servactory::TestKit::Result
Builds a Servactory::TestKit::Result from this config.
-
#dup ⇒ ServiceMockConfig
Creates a deep copy of this config.
-
#failure? ⇒ Boolean
Checks if this is a failure mock.
-
#initialize(service_class:) ⇒ ServiceMockConfig
constructor
Creates a new mock configuration.
-
#result_type_defined? ⇒ Boolean
Checks if result type has been set.
-
#success? ⇒ Boolean
Checks if this is a success mock.
Constructor Details
#initialize(service_class:) ⇒ ServiceMockConfig
Creates a new mock configuration.
47 48 49 50 51 52 53 54 |
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 47 def initialize(service_class:) @service_class = service_class @result_type = nil @method_type = :call @outputs = {} @exception = nil @argument_matcher = nil end |
Instance Attribute Details
#argument_matcher ⇒ Object
Returns the value of attribute argument_matcher.
36 37 38 |
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 36 def argument_matcher @argument_matcher end |
#exception ⇒ Object
Returns the value of attribute exception.
36 37 38 |
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 36 def exception @exception end |
#method_type ⇒ Object
Returns the value of attribute method_type.
36 37 38 |
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 36 def method_type @method_type end |
#outputs ⇒ Object
Returns the value of attribute outputs.
36 37 38 |
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 36 def outputs @outputs end |
#result_type ⇒ Object
Returns the value of attribute result_type.
36 37 38 |
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 36 def result_type @result_type end |
#service_class ⇒ Object
Returns the value of attribute service_class.
36 37 38 |
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 36 def service_class @service_class end |
Instance Method Details
#bang_method? ⇒ Boolean
Checks if this mocks the .call! method.
73 74 75 |
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 73 def bang_method? method_type == :call! end |
#build_argument_matcher(rspec_context) ⇒ Object
Builds RSpec argument matcher from config.
If no matcher specified, builds one from service input names.
104 105 106 107 108 109 110 111 |
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 104 def build_argument_matcher(rspec_context) return argument_matcher if argument_matcher.present? input_names = service_class.info.inputs.keys return rspec_context.no_args if input_names.empty? input_names.to_h { |input_name| [input_name, rspec_context.anything] } end |
#build_result ⇒ Servactory::TestKit::Result
Builds a Servactory::TestKit::Result from this config.
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 87 def build_result result_attrs = outputs.merge(service_class:) if success? Servactory::TestKit::Result.as_success(**result_attrs) else result_attrs[:exception] = exception if exception Servactory::TestKit::Result.as_failure(**result_attrs) end end |
#dup ⇒ ServiceMockConfig
Creates a deep copy of this config.
116 117 118 119 120 121 122 123 124 |
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 116 def dup copy = self.class.new(service_class:) copy.result_type = result_type copy.method_type = method_type copy.outputs = outputs.dup copy.exception = exception copy.argument_matcher = argument_matcher copy end |
#failure? ⇒ Boolean
Checks if this is a failure mock.
66 67 68 |
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 66 def failure? result_type == :failure end |
#result_type_defined? ⇒ Boolean
Checks if result type has been set.
80 81 82 |
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 80 def result_type_defined? !result_type.nil? end |
#success? ⇒ Boolean
Checks if this is a success mock.
59 60 61 |
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 59 def success? result_type == :success end |