class Timecop

Timecop

This is very useful when your app's functionality is dependent on time (e.g. anything that might expire). This will allow us to alter the return value of Date.today, Time.now, and DateTime.now, such that our application code never has to change.

Constants

VERSION

Attributes

active_support[RW]

Public Class Methods

baseline() click to toggle source
# File lib/timecop/timecop.rb, line 78
def baseline
  instance.send(:baseline)
end
baseline=(baseline) click to toggle source
# File lib/timecop/timecop.rb, line 82
def baseline=(baseline)
  instance.send(:baseline=, baseline)
end
freeze(*args, &block) click to toggle source

Allows you to run a block of code and "fake" a time throughout the execution of that block. This is particularly useful for writing test methods where the passage of time is critical to the business logic being tested. For example:

joe = User.find(1)
joe.purchase_home()
assert !joe.mortgage_due?
Timecop.freeze(2008, 10, 5) do
  assert joe.mortgage_due?
end

freeze and travel will respond to several different arguments:

  1. ::freeze

  2. ::freeze

  3. ::freeze

  4. ::freeze

  5. ::freeze(year, month, day, hour=0, minute=0, second=0)

When a block is also passed, Time.now, DateTime.now and Date.today are all reset to their previous values after the block has finished executing. This allows us to nest multiple calls to ::travel and have each block maintain it's concept of "now."

  • Note: ::freeze will actually freeze time. This can cause unanticipated problems if benchmark or other timing calls are executed, which implicitly expect Time to actually move forward.

  • Rails Users: Be especially careful when setting this in your development environment in a rails project. Generators will load your environment, including the migration generator, which will lead to files being generated with the timestamp set by the ::freeze call in your dev environment

Returns the value of the block if one is given, or the mocked time.

# File lib/timecop/timecop.rb, line 51
def freeze(*args, &block)
  send_travel(:freeze, *args, &block)
end
return(&block) click to toggle source

Reverts back to system's Time.now, Date.today and DateTime.now (if it exists) permamently when no block argument is given, or temporarily reverts back to the system's time temporarily for the given block.

# File lib/timecop/timecop.rb, line 89
def return(&block)
  if block_given?
    instance.send(:return, &block)
  else
    instance.send(:unmock!)
    nil
  end
end
return_to_baseline() click to toggle source
# File lib/timecop/timecop.rb, line 98
def return_to_baseline
  instance.send(:return_to_baseline)
  Time.now
end
scale(*args, &block) click to toggle source

Allows you to run a block of code and "scale" a time throughout the execution of that block. The first argument is a scaling factor, for example:

Timecop.scale(2) do
  ... time will 'go' twice as fast here
end

See Timecop#freeze for exact usage of the other arguments

Returns the value of the block if one is given, or the mocked time.

# File lib/timecop/timecop.rb, line 74
def scale(*args, &block)
  send_travel(:scale, *args, &block)
end
travel(*args, &block) click to toggle source

Allows you to run a block of code and "fake" a time throughout the execution of that block. See Timecop#freeze for a sample of how to use (same exact usage syntax)

  • Note: ::travel will not freeze time (as opposed to ::freeze). This is a particularly good candidate for use in environment files in rails projects.

Returns the value of the block if one is given, or the mocked time.

# File lib/timecop/timecop.rb, line 62
def travel(*args, &block)
  send_travel(:travel, *args, &block)
end