Class: Impersonator::ReplayMode

Inherits:
Object
  • Object
show all
Includes:
HasLogger
Defined in:
lib/impersonator/replay_mode.rb

Overview

The state of a recording in replay mode

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasLogger

#logger

Constructor Details

#initialize(recording_path) ⇒ ReplayMode

Returns a new instance of ReplayMode.

Parameters:

  • recording_path (String)

    the file path to the recording file



10
11
12
# File 'lib/impersonator/replay_mode.rb', line 10

def initialize(recording_path)
  @recording_path = recording_path
end

Instance Attribute Details

#recording_pathObject (readonly)

recording file path



7
8
9
# File 'lib/impersonator/replay_mode.rb', line 7

def recording_path
  @recording_path
end

Instance Method Details

#finishObject

Finishes the record session



40
41
42
43
44
45
46
# 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) ⇒ Object

Replays the method invocation

Parameters:

  • impersonated_object (Object, Double)

    not used in replay mode

  • method (MethodInvocation)
  • args (Array<Object>)

    not used in replay mode



26
27
28
29
30
31
32
33
34
35
36
37
# 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

#startObject

Start a replay session



15
16
17
18
19
# 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