aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-01-30 04:19:00 -0800
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-01-30 04:19:00 -0800
commitdb6f69fd2a35dde23ab8a9b8fad6071043ac8954 (patch)
treec36d39f66c220c8041ddd72efff3264275adf4b5
parent7f5466d58299db35a3d320e0b526001ae3be11a7 (diff)
parentfa1f20e6549f962112948f5b3c27d09ab5e5faaf (diff)
downloadrails-db6f69fd2a35dde23ab8a9b8fad6071043ac8954.tar.gz
rails-db6f69fd2a35dde23ab8a9b8fad6071043ac8954.tar.bz2
rails-db6f69fd2a35dde23ab8a9b8fad6071043ac8954.zip
Merge pull request #13884 from rafaelfranca/rm-travel-back
Add `travel_back` to remove stubs from `travel` and `travel_to` and remove auto-rollback after each test case
-rw-r--r--activerecord/test/cases/mixin_test.rb6
-rw-r--r--activesupport/CHANGELOG.md12
-rw-r--r--activesupport/lib/active_support/testing/time_helpers.rb31
-rw-r--r--activesupport/test/test_test.rb16
-rw-r--r--activesupport/test/time_zone_test.rb3
-rw-r--r--guides/source/4_1_release_notes.md4
6 files changed, 58 insertions, 14 deletions
diff --git a/activerecord/test/cases/mixin_test.rb b/activerecord/test/cases/mixin_test.rb
index ad0d5cce27..7ddb2bfee1 100644
--- a/activerecord/test/cases/mixin_test.rb
+++ b/activerecord/test/cases/mixin_test.rb
@@ -6,10 +6,14 @@ end
class TouchTest < ActiveRecord::TestCase
fixtures :mixins
- def setup
+ setup do
travel_to Time.now
end
+ teardown do
+ travel_back
+ end
+
def test_update
stamped = Mixin.new
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 008d71701c..17890b2668 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,3 +1,15 @@
+* Remove behavior that automatically remove the Date/Time stubs, added by `travel`
+ and `travel_to` methods, after each test case.
+
+ Now users have to use the `travel_back` or the block version of `travel` and
+ `travel_to` methods to clean the stubs.
+
+ *Rafael Mendonça França*
+
+* Add `travel_back` to remove stubs from `travel` and `travel_to`.
+
+ *Rafael Mendonça França*
+
* Remove the deprecation about the `#filter` method.
Filter objects should now rely on method corresponding to the filter type
diff --git a/activesupport/lib/active_support/testing/time_helpers.rb b/activesupport/lib/active_support/testing/time_helpers.rb
index 0ee6332d6f..9e0a3d6345 100644
--- a/activesupport/lib/active_support/testing/time_helpers.rb
+++ b/activesupport/lib/active_support/testing/time_helpers.rb
@@ -41,14 +41,8 @@ module ActiveSupport
# Containing helpers that helps you test passage of time.
module TimeHelpers
- def after_teardown #:nodoc:
- simple_stubs.unstub_all!
- super
- end
-
- # Change current time to the time in the future or in the past by a given time difference by
- # stubbing +Time.now+ and +Date.today+. Note that the stubs are automatically removed
- # at the end of each test.
+ # Changes current time to the time in the future or in the past by a given time difference by
+ # stubbing +Time.now+ and +Date.today+.
#
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
# travel 1.day
@@ -67,9 +61,8 @@ module ActiveSupport
travel_to Time.now + duration, &block
end
- # Change current time to the given time by stubbing +Time.now+ and +Date.today+ to return the
- # time or date passed into this method. Note that the stubs are automatically removed
- # at the end of each test.
+ # Changes current time to the given time by stubbing +Time.now+ and +Date.today+ to return the
+ # time or date passed into this method.
#
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
# travel_to Time.new(2004, 11, 24, 01, 04, 44)
@@ -81,7 +74,7 @@ module ActiveSupport
#
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
# travel_to Time.new(2004, 11, 24, 01, 04, 44) do
- # User.create.created_at # => Wed, 24 Nov 2004 01:04:44 EST -05:00
+ # Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
# end
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
def travel_to(date_or_time, &block)
@@ -90,10 +83,22 @@ module ActiveSupport
if block_given?
block.call
- simple_stubs.unstub_all!
+ travel_back
end
end
+ # Returns the current time back to its original state, by removing the stubs added by
+ # `travel` and `travel_to`.
+ #
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
+ # travel_to Time.new(2004, 11, 24, 01, 04, 44)
+ # Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
+ # travel_back
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
+ def travel_back
+ simple_stubs.unstub_all!
+ end
+
private
def simple_stubs
diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb
index 8a71ef4324..0fa08c0e3a 100644
--- a/activesupport/test/test_test.rb
+++ b/activesupport/test/test_test.rb
@@ -162,6 +162,10 @@ class TimeHelperTest < ActiveSupport::TestCase
Time.stubs now: Time.now
end
+ teardown do
+ travel_back
+ end
+
def test_time_helper_travel
expected_time = Time.now + 1.day
travel 1.day
@@ -201,4 +205,16 @@ class TimeHelperTest < ActiveSupport::TestCase
assert_not_equal expected_time, Time.now
assert_not_equal Date.new(2004, 11, 24), Date.today
end
+
+ def test_time_helper_travel_back
+ expected_time = Time.new(2004, 11, 24, 01, 04, 44)
+
+ travel_to expected_time
+ assert_equal expected_time, Time.now
+ assert_equal Date.new(2004, 11, 24), Date.today
+ travel_back
+
+ assert_not_equal expected_time, Time.now
+ assert_not_equal Date.new(2004, 11, 24), Date.today
+ end
end
diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb
index 1107b48460..cd79efbe8c 100644
--- a/activesupport/test/time_zone_test.rb
+++ b/activesupport/test/time_zone_test.rb
@@ -97,6 +97,7 @@ class TimeZoneTest < ActiveSupport::TestCase
assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
travel_to(Time.utc(2000, 1, 2, 5)) # midnight Jan 2 EST
assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
+ travel_back
end
def test_tomorrow
@@ -108,6 +109,7 @@ class TimeZoneTest < ActiveSupport::TestCase
assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].tomorrow
travel_to(Time.utc(2000, 1, 2, 5)) # midnight Jan 2 EST
assert_equal Date.new(2000, 1, 3), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].tomorrow
+ travel_back
end
def test_yesterday
@@ -119,6 +121,7 @@ class TimeZoneTest < ActiveSupport::TestCase
assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].yesterday
travel_to(Time.utc(2000, 1, 2, 5)) # midnight Jan 2 EST
assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].yesterday
+ travel_back
end
def test_local
diff --git a/guides/source/4_1_release_notes.md b/guides/source/4_1_release_notes.md
index 7399bfb5de..4e75bf400c 100644
--- a/guides/source/4_1_release_notes.md
+++ b/guides/source/4_1_release_notes.md
@@ -607,6 +607,10 @@ for detailed changes.
`Time.now` and
`Date.today`. ([Pull Request](https://github.com/rails/rails/pull/12824))
+* Added `ActiveSupport::Testing::TimeHelpers#travel_back`. This method returns
+ the current time to the original state, by removing the stubs added by `travel`
+ and `travel_to`. ([Pull Request](https://github.com/rails/rails/pull/13884))
+
* Added `Numeric#in_milliseconds`, like `1.hour.in_milliseconds`, so we can feed
them to JavaScript functions like
`getTime()`. ([Commit](https://github.com/rails/rails/commit/423249504a2b468d7a273cbe6accf4f21cb0e643))