instance method
travel
Ruby on Rails 5.2.8.1
Since v4.1.16Signature
travel(duration, &block)
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.
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
Parameters
-
durationreq -
blockblock
Source
# File activesupport/lib/active_support/testing/time_helpers.rb, line 78
def travel(duration, &block)
travel_to Time.now + duration, &block
end
Defined in activesupport/lib/active_support/testing/time_helpers.rb line 78
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveSupport::Testing::TimeHelpers