public
Rubygem
Description: A gem providing a unified method to mock Time.now, Date.today, and DateTime.now. It provides "time travel" capabilities, making it dead simple to test time-dependent code by either freezing or moving the concept of now.
Homepage: http://rubyforge.org/projects/johntrupiano/
Clone URL: git://github.com/jtrupiano/timecop.git
name age message
file History.txt Fri Mar 06 19:21:54 -0800 2009 Update History with 0.2.1 changes [jtrupiano]
file LICENSE Fri Mar 06 19:15:06 -0800 2009 Fix poor textile formatting, hyperlink blog pos... [jtrupiano]
file Manifest.txt Tue Dec 23 19:17:01 -0800 2008 Completed the 0.2.0 feature set. Broader testi... [John Trupiano]
file README.textile Fri Mar 06 19:18:25 -0800 2009 README love [jtrupiano]
file Rakefile Sat Mar 07 12:55:12 -0800 2009 Rely on jtrupiano-jeweler 0.9.2 for RubyForge i... [jtrupiano]
file VERSION.yml Fri Mar 06 19:04:30 -0800 2009 Version bump to 0.2.1 [jtrupiano]
directory lib/ Fri Mar 06 18:50:42 -0800 2009 Allow to travel relative to Time.now. If an int... [pietern]
directory test/ Fri Mar 06 18:50:42 -0800 2009 Allow to travel relative to Time.now. If an int... [pietern]
file timecop.gemspec Fri Mar 06 19:04:58 -0800 2009 Regenerated gemspec for version 0.2.1 [jtrupiano]

timecop

  • http://github.com/jtrupiano/timecop

DESCRIPTION

A gem providing simple ways to mock Time.now, Date.today, and DateTime.now. It provides “time travel” and “time freezing” capabilities, making it dead simple to test time-dependent code.

FEATURES

  • Temporarily (or permanently if you prefer) change the concept of Time.now, DateTime.now, and Date.today
  • Timecop api allows arguments to be passed into #freeze and #travel as one of the following: # Time instance # DateTime instance # Date instance # individual arguments (year, month, day, hour, minute, second) # a single integer argument that is interpreted as an offset in seconds from Time.now
  • Nested calls to Timecop#travel and Timecop#freeze are supported—each block will maintain it’s interpretation of now.

SHORTCOMINGS

  • Only fully tested on the 1.8.6 Ruby implementations. 1.8.7 and 1.9.1 should be tested, as well as other flavors (jruby, etc.)

SYNOPSIS

Run a time-sensitive test


  joe = User.find(1)
  joe.purchase_home()
  assert !joe.mortgage_due?
  # move ahead a month and assert that the mortgage is due
  Timecop.freeze(Date.today + 30) do
    assert joe.mortgage_due?
  end

Set the time for the test environment of a rails app—this is particularly helpful if your whole application is time-sensitive. It allows you to build your test data at a single point in time, and to move in/out of that time as appropriate (within your tests)

in config/environments/test.rb


  config.after_initialize do
    # Set Time.now to September 1, 2008 10:05:00 AM
    t = Time.local(2008, 9, 1, 10, 5, 0)
    Timecop.travel(t)
  end

REQUIREMENTS

  • None

INSTALL

  • sudo gem install timecop (latest stable version from rubyforge)
  • sudo gem install jtrupiano-timecop (HEAD of the repo from github)

REFERENCES