instance method
freeze_time
Ruby on Rails 8.1.2
Since v5.2.8.1Signature
freeze_time(date_or_time = Time.now, with_usec: false, &block)
Calls travel_to with date_or_time, which defaults to Time.now. Forwards optional with_usec argument.
Time.current # => Sun, 09 Jul 2017 15:34:49 EST -05:00 freeze_time sleep(1) Time.current # => Sun, 09 Jul 2017 15:34:49 EST -05:00 freeze_time Time.current + 1.day sleep(1) Time.current # => Mon, 10 Jul 2017 15:34:49 EST -05:00
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 # => Sun, 09 Jul 2017 15:34:49 EST -05:00 freeze_time do sleep(1) User.create.created_at # => Sun, 09 Jul 2017 15:34:49 EST -05:00 end Time.current # => Sun, 09 Jul 2017 15:34:50 EST -05:00
Parameters
-
date_or_timeopt = Time.now -
with_useckey = false -
blockblock
Source
# File activesupport/lib/active_support/testing/time_helpers.rb, line 261
def freeze_time(date_or_time = Time.now, with_usec: false, &block)
travel_to date_or_time, with_usec: with_usec, &block
end
Defined in activesupport/lib/active_support/testing/time_helpers.rb line 261
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveSupport::Testing::TimeHelpers