Skip to content

Narnach/freeze_time

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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]

About

FreezeTime can freeze time in tests.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages