diff options
author | प्रथमेश Sonpatki <csonpatki@gmail.com> | 2017-07-10 15:43:37 +0530 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-10 12:13:37 +0200 |
commit | b6300f3ecc79bff29cf9bb804a30fd92403feac1 (patch) | |
tree | 2a7b232aa414e5abf4e602699c1f96914d7eb7e6 /activesupport | |
parent | a35f08f038570fe13be5ac789cbbaf4e3d3e527a (diff) | |
download | rails-b6300f3ecc79bff29cf9bb804a30fd92403feac1.tar.gz rails-b6300f3ecc79bff29cf9bb804a30fd92403feac1.tar.bz2 rails-b6300f3ecc79bff29cf9bb804a30fd92403feac1.zip |
Added time helper method `freeze_time` which is an alias for `travel_to Time.now` (#29681)
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activesupport/lib/active_support/testing/time_helpers.rb | 21 | ||||
-rw-r--r-- | activesupport/test/time_travel_test.rb | 22 |
3 files changed, 47 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index ab5237488a..facd723bc5 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +* 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 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 diff --git a/activesupport/test/time_travel_test.rb b/activesupport/test/time_travel_test.rb index 616015b74e..148aa5ef7f 100644 --- a/activesupport/test/time_travel_test.rb +++ b/activesupport/test/time_travel_test.rb @@ -163,4 +163,26 @@ class TimeTravelTest < ActiveSupport::TestCase 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 |