Dates and Times

Date and time logic has a well-deserved reputation as some of the most complex and irritating logic in any application. Testing calendar logic—including time-based reports, automatic logouts, and “1 day ago” text displays—can be a particular headache, but you can do a couple of things to simplify the time-logic beast.

Part of the Problem

You have a test that uses factories. You’d like to test some time-based code that might be used in a search or report result. This goes in spec/unit/project_spec.rb:

 let(​:runway​) { create(​:project​, ​name: ​​"Project Runway"​,
 start_date: ​​"2018-01-20"​ }
 let(​:greenlight​) { create(​:project​, ​name: ​​"Project Greenlight"​,
 start_date: ​​"2018-02-24"​ }
 let(​:gutenberg​) { create(​ ...

Get Rails 5 Test Prescriptions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.