public
Description: Ruby gem to warp time in tests
Homepage: http://iridesco.com
Clone URL: git://github.com/iridesco/time-warp.git
name age message
file MIT-LICENSE Fri Nov 07 16:47:22 -0800 2008 Update license. [bjhess]
file README.md Fri Jun 05 08:43:43 -0700 2009 Update README credits with links. [bjhess]
file Rakefile Fri Nov 07 16:40:47 -0800 2008 Initial commit. Should be a working plugin. If ... [bjhess]
file init.rb Sun Nov 09 12:33:10 -0800 2008 Move require statements into lib/time_warp.rb s... [bjhess]
directory lib/ Fri Nov 14 13:46:40 -0800 2008 Make pretend_now_is more flexible. It now accep... [bjhess]
directory tasks/ Fri Nov 07 16:40:47 -0800 2008 Initial commit. Should be a working plugin. If ... [bjhess]
directory test/ Fri Nov 14 13:46:40 -0800 2008 Make pretend_now_is more flexible. It now accep... [bjhess]
file time_warp.gemspec Mon Jan 05 13:54:17 -0800 2009 Update gemspec. [bjhess]
README.md

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.