

And that includes me, as I only discovered it this week. So it’s no surprise that not many Rails developers are aware of its existence. TimeHelpers was released with little fanfare in Rails 4.1, by way of a brief sentence at the bottom of the release notes in April 2014. Seemingly less well known, however, is that Ruby on Rails includes its own Timecop alternative with the equally catchy name of “ActiveSupport::Testing::TimeHelpers”. Since its release in 2009 it has been downloaded over 81 million times according to, which is particularly impressive considering it has one single maintainer. Timecop is well used, and well loved, throughout the Ruby community. Timecop serves us well at FreeAgent, we use it extensively with over 1,300 calls to Timecop across more than 500 spec files. This will cause the current time to be constant throughout the test, regardless of what happens with the system clock. This flakiness can be eliminated by including Timecop and calling eeze at the beginning of this test.

However if this test happened to run just as the system clock ticked over to the next second, the model’s created_at timestamp would be one second earlier than Time.now in the expect comparison, causing the test to fail. Most of the time the above test would pass without issue. This is important because without control over the time, flakey tests can emerge in your codebase.Ī very simple example is testing the created_at attribute of an ActiveRecord model: # ActiveRecord will set the model’s created_at field Timecop is the go-to gem for testing time-dependent code, as it allows you to manipulate the current time during test runs.

TL DR – You probably can’t replace Timecop with Rails’ built in TimeHelpers, as TimeHelpers only recreates Timecop’s freeze method, and can’t handle nested travelling.
