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
This one had me frustrated for the past hour. I’ve been writing integration tests for a Rails project and got stuck on an error when I was trying to test that a file upload worked successfully and asserted a redirection was occuring correctly, but ran into the following error:
Expected response to be a <:redirect>, but was <500><"No HTTP_REFERER was set in the request to this action, so
redirect_to :back could not be called successfully. If this
is a test, make sure to specify
request.env[\"HTTP_REFERER\"].">
What a lovely error message to send me on a goose chase trying to set HTTP_REFERER directly on the @request as instructed.
This continued to spit out the same error. I finally stumbled across that held the answer. The HTTP_REFERER is not set the same way in an integration test:
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: