Module: StepperMotor::TestHelper

Defined in:
lib/stepper_motor/test_helper.rb

Instance Method Summary collapse

Instance Method Details

#immediately_perform_single_step(journey, step_name) ⇒ Object

Performs the named step of the journey without waiting for the time to perform the step.

Parameters:

  • journey (StepperMotor::Journey)

    the journey to speedrun

  • step_name (Symbol)

    the name of the step to run

Returns:

  • void



56
57
58
59
60
# File 'lib/stepper_motor/test_helper.rb', line 56

def immediately_perform_single_step(journey, step_name)
  journey.save!
  journey.update!(next_step_name: step_name, next_step_to_be_performed_at: Time.current)
  journey.perform_next_step!
end

#speedrun_journey(journey, time_travel: true, maximum_steps: :reasonable) ⇒ Object

Allows running a given Journey to completion, skipping across the waiting periods. This is useful to evaluate all side effects of a Journey. The helper will ensure that the number of steps performed is equal to the number of steps defined - this way it will not enter into an endless loop. If, after completing all the steps, the journey has neither canceled nor finished, an exception will be raised.

Parameters:

  • journey (StepperMotor::Journey)

    the journey to speedrun

  • time_travel (Boolean) (defaults to: true)

    whether to use ActiveSupport time travel (default: true) Note: When time_travel is true, this method will permanently travel time forward and will not reset it back to the original time when the method exits.

  • maximum_steps (Symbol, Integer) (defaults to: :reasonable)

    how many steps can we take until we assume the journey has hung and fail the test. Default value is :reasonable, which is 10x the number of steps. :unlimited allows "a ton", but can make your test hang if your logic lets a step reattempt indefinitely

Returns:

  • void



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/stepper_motor/test_helper.rb', line 19

def speedrun_journey(journey, time_travel: true, maximum_steps: :reasonable)
  journey.save!
  n_steps = case maximum_steps
  when :reasonable
    journey.step_definitions.length * 10
  when :unlimited
    0xFFFF
  when Integer
    maximum_steps
  else
    raise ArgumentError, "maximum_steps may be :reasonable, :unlimited or an Integer, was #{maximum_steps.inspect}"
  end

  n_steps.times do
    journey.reload
    break if journey.canceled? || journey.finished?

    if time_travel
      # Use time travel to move slightly ahead of the time when the next step should be performed
      next_step_time = journey.next_step_to_be_performed_at
      travel_to(next_step_time + 1.second)
    else
      # Update the journey's timestamp to bypass waiting periods
      journey.update(next_step_to_be_performed_at: Time.current)
    end
    journey.perform_next_step!
  end
  journey.reload
  journey_did_complete = journey.canceled? || journey.finished?
  raise "Journey #{journey} did not finish or cancel after performing #{n_steps} steps" unless journey_did_complete
end