Class: Servactory::TestKit::Rspec::Helpers::ServiceMockConfig

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(service_class:) ⇒ ServiceMockConfig

Creates a new mock configuration.

Parameters:

  • service_class (Class)

    The service class to mock



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_matcherObject

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

#exceptionObject

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_typeObject

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

#outputsObject

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_typeObject

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_classObject

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.

Returns:

  • (Boolean)

    True if method_type is :call!



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.

Parameters:

  • rspec_context (Object)

    The RSpec test context

Returns:

  • (Object)

    RSpec argument matcher



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_resultServactory::TestKit::Result

Builds a Servactory::TestKit::Result from this config.

Returns:



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

#dupServiceMockConfig

Creates a deep copy of this config.

Returns:



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.

Returns:

  • (Boolean)

    True if result_type is :failure



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.

Returns:

  • (Boolean)

    True if result_type is not nil



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.

Returns:

  • (Boolean)

    True if result_type is :success



59
60
61
# File 'lib/servactory/test_kit/rspec/helpers/service_mock_config.rb', line 59

def success?
  result_type == :success
end