It really did, making me unproductive around midnight as some of my unit test started failing. To be honest, I did fall asleep most of the times just by counting the failed assertions.
Now when a Rails test starts failing at a particular time during the day, there's probably some
Time.zonemisuse. But first let's do a recap here.
Testing time sensitive models or any time related behavior will sooner or later bend us to use
Time.now. There's nothing wrong with that, as long as we keep in mind that when a model with a
Timeattribute value is saved and re-read there's a high chance it won't equal to the initial
Timeinstance set. Most database vendor
:datetimecolumns only use second precision and thus our time looses it's microsecond precision. Easy help, just use a custom time equality asserter:
However, this is not the only difference, after setting and reading the time attribute it holds a
ActiveSupport::TimeWithZoneinstance. Rails performs this "conversion" to enable representing times in any time zone, as the standard
Timeis limited to UTC and the system zone. Well of course such times are equal no matter of the time zone. Issues might arise when times in different zones are converted to dates.
This starts to happen if You've configured
ActiveRecordto store times in UTC (instead of the local time) which I do found very reasonable, as well as setting
Time.zoneto default to UTC (and adjusting it per request). For example, if You're GMT+0200 positive and write
Time.nowto an attribute, read it and convert to a
Dateobject, it will start to return yesterday around midnight:
>> t1 = Time.parse "Tue May 04 00:00:00 +0200 2010" => Tue May 04 00:00:00 +0200 2010 >> t2 = t1.in_time_zone 'UTC' => Mon, 03 May 2010 22:00:00 UTC +00:00 >> t1.class => Time >> t2.class => ActiveSupport::TimeWithZone >> t1.to_date => Tue, 04 May 2010 >> t2.to_date => Mon, 03 May 2010
t2is "simulating" the
ActiveRecordconversion mechanism. Once we realize all that the test failures might be avoided by setting the
Time.zoneto your local time during tests:
And all the tests lived happily ever after ...