github github
  • Home
  • Pricing and Signup
  • Training
  • Gist
  • Blog
  • Login

jtrupiano / timecop

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 279
    • 8
  • Source
  • Commits
  • Network (8)
  • Issues (3)
  • Downloads (9)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Switch Branches (2)
    • master ✓
    • rewind
  • Switch Tags (9)
    • v0.3.5
    • v0.3.4.rc2
    • v0.3.4.rc1
    • v0.3.4
    • v0.3.2
    • v0.3.1
    • v0.3.0
    • v0.2.1
    • 0.1.0
  • Branch List
Sending Request…

A gem providing “time travel” and “time freezing” capabilities, making it dead simple to test time-dependent code. It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call. — Read more

  Cancel

http://www.smartlogicsolutions.com/open-source-projects

  Cancel
  • HTTP
  • Git Read-Only

This URL has Read+Write access

Document 0.3.5 release, update gemspec 
jtrupiano (author)
Mon Jun 07 08:20:44 -0700 2010
commit  4acf2e09f0fe97818264
tree    bafd9b8f487044c9acd6
parent  17ac4d336b0cf04903de
timecop /
name age
history
message
file History.rdoc Mon Jun 07 08:20:44 -0700 2010 Document 0.3.5 release, update gemspec [jtrupiano]
file LICENSE Fri Mar 06 19:15:06 -0800 2009 Fix poor textile formatting, hyperlink blog pos... [jtrupiano]
file README.rdoc Mon Dec 07 13:35:03 -0800 2009 Add reference in README to 0.3.4 release [jtrupiano]
file Rakefile Sat Nov 28 15:20:59 -0800 2009 Update rubyforge doc release task with History.... [jtrupiano]
file VERSION.yml Mon Dec 07 13:48:32 -0800 2009 Bump version to 0.3.5 immediately following 0.3... [jtrupiano]
directory lib/ Mon Jun 07 08:13:51 -0700 2010 Clone returned time instance. [lmarburger]
directory test/ Mon Jun 07 08:13:51 -0700 2010 Include lib directory when executing tests. [lmarburger]
file timecop.gemspec Mon Jun 07 08:20:44 -0700 2010 Document 0.3.5 release, update gemspec [jtrupiano]
README.rdoc

timecop

  • Source
  • Documentation

DESCRIPTION

A gem providing "time travel" and "time freezing" capabilities, making it dead simple to test time-dependent code. It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call.

INSTALL

  gem install timecop

FEATURES

  • Freeze time to a specific point.
  • Travel back to a specific point in time, but allow time to continue moving forward from there.
  • No dependencies, can be used with any ruby project
  • 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 its interpretation of now.

USAGE

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 (at this instant), but allow it to move forward
    t = Time.local(2008, 9, 1, 10, 5, 0)
    Timecop.travel(t)
  end

The difference between Timecop.freeze and Timecop.travel

#freeze is used to statically mock the concept of now. As your program executes, Time.now will not change unless you make subsequent calls into the Timecop API. #travel, on the other hand, computes an offset between what we currently think Time.now is (recall that we support nested traveling) and the time passed in. It uses this offset to simulate the passage of time. To demonstrate, consider the following code snippets:

  new_time = Time.local(2008, 9, 1, 12, 0, 0)
  Timecop.freeze(new_time)
  sleep(10)
  new_time == Time.now # ==> true

  Timecop.return # "turn off" Timecop
  Timecop.travel(new_time)
  sleep(10)
  new_time == Time.now # ==> false

REFERENCES

  • 0.3.4 release
  • 0.3.0 release
  • 0.2.0 release
  • 0.1.0 release
Dedicated Server Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
  • Blog
  • Support
  • Training
  • Job Board
  • Shop
  • Contact
  • API
  • Status
  • © 2010 GitHub Inc. All rights reserved.
  • Terms of Service
  • Privacy
  • Security
  • English
  • Deutsch
  • Français
  • 日本語
  • Português (BR)
  • 中文
  • See all available languages

Your current locale selection: English. Choose another?

  • English
  • Afrikaans
  • Català
  • Čeština
  • Deutsch
  • Español
  • Français
  • Hrvatski
  • Indonesia
  • Italiano
  • 日本語
  • Nederlands
  • Norsk
  • Polski
  • Português (BR)
  • Српски
  • Svenska
  • 中文