From b6300f3ecc79bff29cf9bb804a30fd92403feac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=AA=E0=A5=8D=E0=A4=B0=E0=A4=A5=E0=A4=AE=E0=A5=87?= =?UTF-8?q?=E0=A4=B6=20Sonpatki?= Date: Mon, 10 Jul 2017 15:43:37 +0530 Subject: Added time helper method `freeze_time` which is an alias for `travel_to Time.now` (#29681) --- .../lib/active_support/testing/time_helpers.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'activesupport/lib/active_support/testing') diff --git a/activesupport/lib/active_support/testing/time_helpers.rb b/activesupport/lib/active_support/testing/time_helpers.rb index eaccbb97eb..d72d82d7c4 100644 --- a/activesupport/lib/active_support/testing/time_helpers.rb +++ b/activesupport/lib/active_support/testing/time_helpers.rb @@ -161,6 +161,27 @@ module ActiveSupport 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 -- cgit v1.2.3