O'Reilly logo

Rails 4 Test Prescriptions by Noel Rappin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

We have a YAML file with some projects:

 
runway:
 name: Project Runway
 
start_date: ​2015-01-20
 
 
greenlight:
 name: Project Greenlight
 
start_date: ​2015-02-04
 
 
gutenberg:
 name: Project Gutenberg
 
start_date: ​2015-01-31

We’d like to test some time-based code that might be used in a search or report result; this goes in test/unit/project_test.rb:

 
it ​"finds recently started projects" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required