This is a post in my series on things I’ve remarked on in code review.
Do you see a problem with this test?
1 2 3 4 5 6 7
What happens if this test runs right around midnight, or around a daylight saving transition?
event might be created just as Monday ends, and
EventFinder.find_all_on_date may run just as Tuesday starts.
So we’d create a Monday event but search for Tuesday events, and the test will fail.
In this case the fix is trivial:
1 2 3 4 5 6 7 8
In more complex cases, you may need to call a time cop and ask them to freeze time for the duration of your test.
Admittedly, this isn’t a major issue. I see these types of tests failures a few times a year – it doesn’t happen often if you don’t often run your tests around midnight.
But it’s also easy to avoid once you’re aware of it, so I recommend writing tests not to be midnight fragile from now on.