Narnach / freeze_time

FreezeTime can freeze time in tests.

This URL has Read+Write access

Wes Oldenbeuving (author)
Mon Sep 28 03:54:17 -0700 2009
commit  a978e422f4ee0ccc103760c22e8725fbbfc9583f
tree    d0313401e9ab038eb5a1d5db34ca7783483911c5
parent  b01885b0fa5465f1d6eaad12febae7422c0b1138
readme.rdoc

FreezeTime

Every once in a while you have a need to freeze time in your tests or specs. Usually the code that does it is ugly code, like in this example that uses rspec:

  frozen_time = Time.now
  Time.stub!(:now).and_return(frozen_time)
  Foo.bar.should == frozen_time

You need to read three dense lines to understand that time is frozen and that Foo.bar should return the current time. What if this could be a lot more intention revealing?

  freeze_time do |time|
    Foo.bar.should == time
  end

FreezeTime helps you test code that requires time to be frozen. It does this by implementing Object#freeze_time, which allows you to freeze time for the duration of the block. This makes your code more intention revealing and a lot more pleasant to look at.

Installation

If you are using gemcutter.org, installation is easy:

  gem install freeze_time

The alternative is to build and install the gem by hand:

  git clone git://github.com/Narnach/freeze_time.git
  cd freeze_time
  rake install

There are no external dependencies and it should be compatible with all testing frameworks.

Contributors

Wes Oldenbeuving [github.com/Narnach]