Posted 29 days ago. Visible to the public. Repeats. Linked content.

ActiveSupport includes Timecop-like helpers

ActiveSupport (since 4.1) includes test helpers to manipulate time, just like the Timecop gem:

  • To travel a relative amount of time, use travel:

    Copy
    travel 1.day
  • To travel to a specific moment in time, use travel_to:

    Copy
    travel_to 1.hour.from_now
  • To freeze a specific time, use freeze_time (ActiveSupport 5.2+):

    Copy
    freeze_time 1.hour.from_now

All those methods may also receive a block to call and restore time afterwards. If you don't provide a block, you must call travel_back or unfreeze_time once you are done.

Note that we love Timecop and that it may be better suited for some use cases (for example, it includes Timecop.scale and a Timecop.safe_mode configuration setting), but for most projects you actually may not need Timecop.

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Arne Hartherz
Last edit:
24 days ago
by Judith Roth
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more