Time Travel

Time Travel is a Rails plugin that makes it easy to write tests or specs for time-dependent code. It provides the at_time function:

at_time("9 March 2018 2:32 UTC") do
  ...
end

Inside the block, Time.now will return the given time. The time will be restored to normal system time when the block exits.

The at_time function can take a Time object, or a String to parse into a time.

Example

Here's a test from one of my apps that makes sure login sessions time out if they're idle for more than 2 hours:

class SessionTest < ActionController::IntegrationTest

  def test_should_time_out_idle_sessions_after_2_hours
    post "/login", :name => "fred", :password => "secret"
    get "/admin"
    assert_response :success
    
    at_time(122.minutes.from_now) do
      get "/admin"
      assert_redirected_to "/login"
    end
  end
  
end

Installation

ruby script/plugin install git://github.com/notahat/time_travel.git