Timecop 0.3.4 has just been released. To install simply run: gem install timecop.
Timecop is a RubyGem 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.
Documentation is hosted at . The source code is hosted at .
Timecop 0.3.0 has just been released. To install simply run: gem install timecop.
Timecop is a RubyGem 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.
Documentation is on . The source code is hosted at .
Updates include:
API
Completely remove Timecop#unset_all (deprecated by Timecop#return in 0.2.0)
Return Time.now from #freeze, #travel and #return — code contributed by Keith Bennett ()
Maintenance
Fix bug that left Time#mock_time set in some instances
Upped build dependency to jeweler ~> 1.2.1
Don’t pollute top-level namespace with classes/constants
Documentation
Clearer examples in the README, better description in the gemspec
I just released version 0.2.0 of this evening (morning).
The primary feature added was the distinction between “freezing” and “rebasing” time. In 0.1.0, Timecop.travel would actually freeze time. This is no longer the case. Rather, a time offset will be calculated, and a running clock is simulated by always offsetting the time returned by Time.now (and friends) by the original offset.
(Note that time can still be frozen with Timecop.freeze.)
The API mentioned in this blog post is specific to v0.1.0. The latest version is v0.2.0. See the blog post announcement, the , or the .
This past weekend I released v0.1.0 of the gem. Timecop makes it dead simple to travel through or freeze time for the sake of creating a predictable and ultimately testable scenario.
The gem is derived from a plugin I wrote a while back to achieve more or less the same functionality for an extremely time-sensitive application. My goals for the gem included:
Drop-in-ability: The primary goal is to allow your app to continue to use Time.now, Date.today and DateTime.now as normal within your application. No overloading of functions with optional arguments (a la today=Date.today) just so you can write test cases.
Environment independence: I wanted the gem to work (a) w/ rails (ActiveSupport actually), (b) w/ plain ruby when the ‘date’ library has been loaded, and (c) w/ plain ruby when the ‘date’ library had not been loaded.
Library independence: I could have utilized mocha to achieve the mocking functionality found under the hood, but because I wanted this to work with plain vanilla ruby, libraries like mocha are out.
Short-term time travel: I wanted to expose the ability to temporarily change the concept of “now.” This is particularly helpful when writing tests where time needs to pass.
Long-range time travel: I wanted to expose the ability to change the concept of “now” for an indeterminate period of time. This is particularly helpful when setting up a rails test environment along with the test data.
Nested time travel: I wanted to provide the ability to nest traveling, allowing the state to be kept within each block (we’ll see an example later).
The gem is hosted on RubyForge and can be installed by simply running: