Class: Servactory::TestKit::Rspec::Helpers::MockExecutor

Inherits:
Object
  • Object
show all
Includes:
Concerns::ErrorMessages
Defined in:
lib/servactory/test_kit/rspec/helpers/mock_executor.rb

Overview

Executes RSpec stubbing based on mock configurations.

## Purpose

MockExecutor translates ServiceMockConfig objects into actual RSpec stub setups using ‘allow(…).to receive(…)`. It handles single and sequential call scenarios, applying appropriate return behaviors.

## Usage

Typically used internally by ServiceMockBuilder:

“‘ruby executor = MockExecutor.new(

service_class: MyService,
configs: [config1, config2],
rspec_context: self

) executor.execute “‘

## Execution Strategies

  • **Single Config** - uses ‘and_return` or `and_raise` directly

  • **Sequential Returns** - uses ‘and_return(*values)` for multiple values

  • **Sequential with Raises** - uses ‘and_invoke(*callables)` for mixed behavior

## Architecture

Works with:

  • ServiceMockConfig - provides configuration for each stub

  • ServiceMockBuilder - creates executor with configs

  • RSpec Context - provides allow/receive/etc. methods

Instance Method Summary collapse

Constructor Details

#initialize(service_class:, configs:, rspec_context:) ⇒ MockExecutor

Creates a new mock executor.

Parameters:

  • service_class (Class)

    The Servactory service class to stub

  • configs (Array<ServiceMockConfig>)

    Configurations for each call

  • rspec_context (Object)

    RSpec example context with stubbing methods



48
49
50
51
52
# File 'lib/servactory/test_kit/rspec/helpers/mock_executor.rb', line 48

def initialize(service_class:, configs:, rspec_context:)
  @service_class = service_class
  @configs = configs
  @rspec_context = rspec_context
end

Instance Method Details

#executevoid

This method returns an undefined value.

Executes the stub setup based on configurations.

Validates all configs first, then applies appropriate stubbing strategy (single or sequential).

Raises:

  • (ArgumentError)

    If any config is invalid



61
62
63
64
65
66
67
68
69
# File 'lib/servactory/test_kit/rspec/helpers/mock_executor.rb', line 61

def execute
  validate_configs!

  if sequential?
    execute_sequential
  else
    execute_single
  end
end