unit testing and time travel

Sometimes the solution for a thorny problem is easy. If you are writing unit tests for a piece of code that behaves differently before and after a certain point of time you wish you could do time travel with the computer you are using to run the tests. Adjusting the system date often is inconvenient. A lot of processes depend on proper timekeeping by the operating system and often a regular user cannot adjust time for simple lack of authorization. Take my advice and stay away from the system clock.

The solution is to use Joda Time. It has a class called DateTimeUtils with a static method setCurrentMillisFixed that effectively fixes the system time for the Joda Time library to a certain value. Returning to the present is done by calling setCurrentMillisSystem after you are done time traveling

    public void before() {
        LocalDate pointInTime = new LocalDate().minusDays(10);
        DateTimeUtils.setCurrentMillisFixed(pointInTime.toDateTime(new LocalTime(0, 0, 0, 0)).getMillis());

    public void after() {



condition and return != condition && return

In dealing with the wisdom of rubocop to sanitize code I ran into the following: If you write 

redirect_to_somewhere and return

then the redirect will be followed by leaving the controller method but if you change that into 

redirect_to_somewhere && return

 as per rubocop’s instructions then it fails. Odd. What exactly is the difference between the two exactly? Of course the construct looks like a frequently used Bash idiom and in this case the two statements should be just on separate lines:


However, http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/ learns us that there is a difference in precedence and that we really should use the &&-operator only