instance method freeze_time

Ruby on Rails 7.1.6

Since v5.2.8.1

Available in: v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

freeze_time(with_usec: false, &block)

Calls travel_to with 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

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

with_usec key = false
block block
Source
# File activesupport/lib/active_support/testing/time_helpers.rb, line 256
      def freeze_time(with_usec: false, &block)
        travel_to Time.now, with_usec: with_usec, &block
      end

Defined in activesupport/lib/active_support/testing/time_helpers.rb line 256 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveSupport::Testing::TimeHelpers

Type at least 2 characters to search.

↑↓ navigate · open · esc close