Module: Timecop::Rspec

Defined in:
lib/timecop/rspec.rb,
lib/timecop/rspec/version.rb,
lib/timecop/rspec/traveler.rb,
lib/timecop/rspec/travel_log.rb,
lib/timecop/rspec/time_machine.rb,
lib/timecop/rspec/example_decorator.rb,
lib/timecop/rspec/sequential_time_machine.rb

Overview

RSpec integration for the timecop gem.

Provides helpers to run examples with frozen or traveling time, and an optional global time that applies across examples when configured via ENV.

Environment variables:

  • GLOBAL_TIME_TRAVEL_TIME: String representation of a time (preferred)

  • GLOBAL_TIME_TRAVEL_DATE: String representation of a date (fallback)

Defined Under Namespace

Modules: Version Classes: ExampleDecorator, SequentialTimeMachine, TimeMachine, TravelLog, Traveler

Constant Summary collapse

VERSION =

Convenience constant aliasing Version::VERSION

Returns:

  • (String)
Version::VERSION

Class Method Summary collapse

Class Method Details

.global_timeTime

The globally configured time parsed from ENV.

Returns:

  • (Time)

    the parsed time

Raises:

  • (ArgumentError)

    if ENV contains an unparsable time/date string



41
42
43
# File 'lib/timecop/rspec.rb', line 41

def global_time
  @global_time ||= Time.parse(global_time_travel_string)
end

.global_time_configured?Boolean

Whether a global time has been configured via ENV.

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/timecop/rspec.rb', line 33

def global_time_configured?
  str = global_time_travel_string
  !(str.nil? || str == "")
end

.time_machine(sequential: false) ⇒ Timecop::Rspec::SequentialTimeMachine, Timecop::Rspec::TimeMachine

Selects a time machine strategy.

Parameters:

  • sequential (Boolean) (defaults to: false)

    when true, uses a sequential strategy that can continue a travel across examples; when false, uses a simple per example strategy.

Returns:



23
24
25
26
27
28
29
# File 'lib/timecop/rspec.rb', line 23

def time_machine(sequential: false)
  if sequential
    SequentialTimeMachine.instance
  else
    TimeMachine.instance
  end
end