class Impersonator::ReplayMode

The state of a {Recording recording} in replay mode

Attributes

recording_path[R]

recording file path

Public Class Methods

new(recording_path) click to toggle source

@param [String] recording_path the file path to the recording file

# File lib/impersonator/replay_mode.rb, line 10
def initialize(recording_path)
  @recording_path = recording_path
end

Public Instance Methods

finish() click to toggle source

Finishes the record session

# File lib/impersonator/replay_mode.rb, line 40
def finish
  unless @method_invocations.empty?
    raise Impersonator::Errors::MethodInvocationError,
          "Expecting #{@method_invocations.length} method invocations"\
          " that didn't happen: #{@method_invocations.inspect}"
  end
end
invoke(_impersonated_object, method, _args) click to toggle source

Replays the method invocation

@param [Object, Double] impersonated_object not used in replay mode @param [MethodInvocation] method @param [Array<Object>] args not used in replay mode

# File lib/impersonator/replay_mode.rb, line 26
def invoke(_impersonated_object, method, _args)
  method_invocation = @method_invocations.shift
  unless method_invocation
    raise Impersonator::Errors::MethodInvocationError, 'Unexpected method invocation received:'\
          "#{method}"
  end

  validate_method_signature!(method, method_invocation.method_instance)
  replay_block(method_invocation, method)

  method_invocation.return_value
end
start() click to toggle source

Start a replay session

# File lib/impersonator/replay_mode.rb, line 15
def start
  logger.debug 'Replay mode'
  @replay_mode = true
  @method_invocations = YAML.load_file(recording_path)
end

Private Instance Methods

replay_block(recorded_method_invocation, method_to_replay) click to toggle source
# File lib/impersonator/replay_mode.rb, line 50
def replay_block(recorded_method_invocation, method_to_replay)
  block_spy = recorded_method_invocation.method_instance.block_spy
  block_spy&.block_invocations&.each do |block_invocation|
    method_to_replay.block.call(*block_invocation.arguments)
  end
end
validate_method_signature!(expected_method, actual_method) click to toggle source
# File lib/impersonator/replay_mode.rb, line 57
    def validate_method_signature!(expected_method, actual_method)
      unless actual_method == expected_method
        raise Impersonator::Errors::MethodInvocationError, <<~ERROR
          Expecting:
            #{expected_method}
          But received:
            #{actual_method}
        ERROR
      end
    end