New Rails 4.1 Travel_to Test Helper
I’ve recently had the good fortune of working on a greenfield Rails app. The app is heavily dependent on times and recurring events (weekly). Naturally, I dragged in the timecop gem to handle freezing time, so my I could properly assert that certain events took place in the tests.
With the release of Rails 4.1, the time stubbing method
travel_to was added. This new helper method forces the current time to whatever you specify, allowing you to make asserts against a historical time, or week in my case.
Timecop performed valiantly and did more than I ever needed (I generally only used the
freeze method). However, dragging in another gem was the last thing I wanted to do for something relatively simple.
Prior to utilizing the changes in Rails 4.1, one of my tests looked like:
Note: Chronic is a gem that helps create dates/times by using standard language.
I think I first heard about some additional testing methods from my friend Eric Steele. He mentioned them in one of our various conversations about testing (he’s writing a book titled What Do I Test?).
Towards the bottom, though, was a feature barely mentioned or written about:
TimeHelpers test module adds the
travel_back methods to assist in stubbing
travel_to method with the example above results in the following:
Because I’m using the
travel_to method with a block, there’s no need to use the
travel_back method to reset time back to the current time. Supply a block automatically calls
While the change doesn’t look significant, it removed my need for the
Timecop gem entirely.
Additionally, the same test could be written as:
Rails is well-known for conventions.
travel_to is a great addition to the framework that would’ve otherwise required dragging in additional dependencies.
Next time you look at the release notes for a project, don’t skip over the less featured functionality. For all you know, it could be something that saves you time and frustration that commonly occurs in your projects.