rails/rails — pull request #12824 by sikachu (2013-11-09 18:51:46+00:00)
Title: Add `#travel` and `#travel_to` to `ActiveSupport::TestCase`

Add `ActiveSupport::Testing::TimeHelpers#travel` and `#travel_to`. These methods change current time to the given time or time difference by stubbing `Time.now` and `Date.today` to return the time or date after the difference calculation, or the time or date that got passed into the method respectively. These methods also accepts a block, which will return current time back to its original state at the end of the block.

Time.travel 1.day
Time.travel -1.day

Time.travel 1.day do
User.create.created_at #=> 1 day from now
end

Time.travel_to Time.new(2004, 11, 24, 01, 04, 44)
Time.travel_to Date.new(2004, 11, 24)

Time.travel_to Time.new(2004, 11, 24, 01, 04, 44) do
User.create.created_at #=> 2004-11-24 01:04:44 -0500
end

This module is included in `ActiveSupport::TestCase` automatically.

/cc @dhh, and please do grammar check for the doc for me, @fxn.

---

Also, I was about to add a guide section for this, but I couldn't find a good place to add it. Any suggestions are welcomed!