提交 b6300f3e 编写于 作者: प्रथमेश Sonpatki 提交者: David Heinemeier Hansson

Added time helper method `freeze_time` which is an alias for `travel_to Time.now` (#29681)

上级 a35f08f0
* Add `freeze_time` helper which freezes time to `Time.now` in tests.
*Prathamesh Sonpatki*
* Default `ActiveSupport::MessageEncryptor` to use AES 256 GCM encryption.
On for new Rails 5.2 apps. Upgrading apps can find the config as a new
......
......@@ -161,6 +161,27 @@ def travel_back
simple_stubs.unstub_all!
end
# Calls `travel_to` with `Time.now`.
#
# 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
def freeze_time(&block)
travel_to Time.now, &block
end
private
def simple_stubs
......
......@@ -163,4 +163,26 @@ def test_time_helper_travel_with_time_subclass
assert_equal DateTime.now.to_s, DateTimeSubclass.now.to_s
end
end
def test_time_helper_freeze_time
expected_time = Time.now
freeze_time
sleep(1)
assert_equal expected_time.to_s(:db), Time.now.to_s(:db)
ensure
travel_back
end
def test_time_helper_freeze_time_with_block
expected_time = Time.now
freeze_time do
sleep(1)
assert_equal expected_time.to_s(:db), Time.now.to_s(:db)
end
assert_operator expected_time.to_s(:db), :<, Time.now.to_s(:db)
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册