method
travel

v7.1.3.4 -
Show latest stable
-
0 notes -
Class: TimeHelpers
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (12)
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (9)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (38)
- 7.1.3.4 (0)
- What's this?
travel(duration, with_usec: false, &block)
public
Changes current time to the time in the future or in the past by a given time difference by stubbing Time.now, Date.today, and DateTime.now. The stubs are automatically removed at the end of the test.
Note that the usec for the resulting time will be set to 0 to prevent rounding errors with external services, like MySQL (which will round instead of floor, leading to off-by-one-second errors), unless the with_usec argument is set to true.
Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00 travel 1.day Time.current # => Sun, 10 Nov 2013 15:34:49 EST -05:00 Date.current # => Sun, 10 Nov 2013 DateTime.current # => Sun, 10 Nov 2013 15:34:49 -0500
This method also accepts a block, which will return the current time back to its original state at the end of the block:
Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00 travel 1.day do User.create.created_at # => Sun, 10 Nov 2013 15:34:49 EST -05:00 end Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00