Class: Timecop::Rspec::Traveler

Inherits:
Object
  • Object
show all
Defined in:
lib/timecop/rspec/traveler.rb

Overview

Runs an example, optionally continuing a travel across invocations.

Instance Method Summary collapse

Constructor Details

#initialize(example, travel_log) ⇒ Traveler

Returns a new instance of Traveler.

Parameters:



7
8
9
10
# File 'lib/timecop/rspec/traveler.rb', line 7

def initialize(example, travel_log)
  @example = example
  @travel_log = travel_log
end

Instance Method Details

#runObject

Executes the example within the appropriate Timecop context. If the method is :travel, the starting time may be adjusted based on prior trips recorded in the travel log.

Returns:

  • (Object)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/timecop/rspec/traveler.rb', line 17

def run
  method = example.timecop_method
  time = example.timecop_time

  if method == :travel
    time = travel_log.resume_or_new_trip(
      example.timecop_method, example.timecop_time
    )
  end

  ::Timecop.public_send(method, time) do
    begin
      example.run
    ensure
      travel_log.pause_trip if method == :travel
    end
  end
end