A friend of a friend has
trouble testing timestamps.
I tried replying there, but don’t think my answer got through so here it is.
The short answer: all time operations should not use the wall clock, but a
system time object that you create. In production, the system time object
returns wall clock time. In testing, you replace/mock/modify it so that it
uses the time you give it. In other words, it becomes a clock that you can
freeze and re-set whenever you need to.
Here is an example SystemClock class in Ruby. All of your classes should use
this class to get the current date and time instead of using the Time or
Date classes directly.
Here is the mock used during testing. You can set the time using this mock
object and it will not change until it is set again.
So in your normal production code you get the current time by calling
=SystemClock.time= instead of =Time.now=. In your test code, you’d do
something like this: