github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

iridesco / time-warp

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 53
    • 3
  • Source
  • Commits
  • Network (3)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Tree: 884623f

click here to add a description

click here to add a homepage

  • Branches (1)
    • master
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Ruby gem to warp time in tests — Read more

  cancel

http://iridesco.com

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Update README credits with links. 
bjhess (author)
Fri Jun 05 08:43:43 -0700 2009
commit  884623fec90687c7f3c28b8e8074a39c96946cbb
tree    4f0b3ee485d204a490474dbd37bdbfa46240a1ce
parent  d9cb3820136f9ea2e737a732b3291483929c7fd6
time-warp / README.md README.md
100644 89 lines (59 sloc) 3.412 kb
edit raw blame history

time_warp

When writing tests, it is often desirable to bend time in order to test limits and edges of the day. It is especially useful to warp time to test results across the timezones of the world. Manipulating time is also useful to assure a day of the week, month or year every time the test runs.

Some may say "Why not just mock Time#now?" I see the point, but I find mocking around with baseline Ruby classes to be asking for trouble. Eventually unusual behavior will rear its head and a day will be lost debugging tests - the most excruciating debugging one can be subjected to.

Installation

Plugin:

$ script/plugin install git://github.com/iridesco/time_warp.git

Gem:

$ gem sources -a http://gems.github.com
$ sudo gem install iridesco-time-warp

Gem config in a Rails app. environment.rb:

config.gem 'iridesco-time-warp', :lib => 'time_warp', :source => "http://gems.github.com"

Example

And now a contrived example. In this case, the goal is to let the full mechanics of Rails execute. Yes, this test will even hit the database! The goal is to assure a particular day of week when each test method executes:

require File.dirname(__FILE__) + '/../test_helper'
class CompanyTest < Test::Unit::TestCase

  def setup
    @company = companies(:acme)
  end

  def test_should_find_company_needing_reminded_today
    pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
      @company.reminder_day = 'Thursday'
      @company.save
      companies = Company.find_companies_needing_reminded_today
      assert_equal true, companies.include?(@company)
    end
  end

  def test_should_not_find_company_needing_reminded_tomorrow
    pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
      @company.reminder_day = 'Friday'
      @company.save
      companies = Company.find_companies_needing_reminded_today
      assert_equal false, companies.include?(@company)
    end
  end

  def test_should_not_find_company_needing_reminded_yesterday
    pretend_now_is(Time.utc(2008,"jul",24,20)) do #=> Thu Jul 24 20:00:00 UTC 2008
      @company.reminder_day = 'Wednesday'
      @company.save
      companies = Company.find_companies_needing_reminded_today
      assert_equal false, companies.include?(@company)
    end
  end
end

Notes

The pretend_now_is method may also be called with the arguments for the Time#utc call, rather than a Time argument. So:

pretend_now_is(Time.utc(2008,"jul",24,20)) do
  # Shifted code
end

Becomes:

pretend_now_is(2008,"jul",24,20) do
  # Shifted code
end

Also, pretend_now_is should impact ActiveSupport generated Date extensions such as Date.today, Date.tomorrow, and so on.

Credits

The creation of this plugin is a direct result of Jason M. Felice's snippet (and ensuing discussion). The snippet can be found at DZone.

Further discussion of this snippet's evolution may be found at Barry Hess's blog.

time_warp is maintained and funded by Iridesco.

Copyright (c) 2008 Barry Hess, Iridesco. Released under the MIT license.

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server