mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add travel_back
to remove stubs from travel
and travel_to
This commit is contained in:
parent
7f5466d582
commit
6b16c27881
4 changed files with 33 additions and 3 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
* Add `travel_back` to remove stubs from `travel` and `travel_to`.
|
||||||
|
|
||||||
|
*Rafael Mendonça França*
|
||||||
|
|
||||||
* Remove the deprecation about the `#filter` method.
|
* Remove the deprecation about the `#filter` method.
|
||||||
|
|
||||||
Filter objects should now rely on method corresponding to the filter type
|
Filter objects should now rely on method corresponding to the filter type
|
||||||
|
|
|
@ -42,7 +42,7 @@ module ActiveSupport
|
||||||
# Containing helpers that helps you test passage of time.
|
# Containing helpers that helps you test passage of time.
|
||||||
module TimeHelpers
|
module TimeHelpers
|
||||||
def after_teardown #:nodoc:
|
def after_teardown #:nodoc:
|
||||||
simple_stubs.unstub_all!
|
travel_back
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ module ActiveSupport
|
||||||
#
|
#
|
||||||
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
|
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
|
||||||
# travel_to Time.new(2004, 11, 24, 01, 04, 44) do
|
# 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
|
# end
|
||||||
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
|
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
|
||||||
def travel_to(date_or_time, &block)
|
def travel_to(date_or_time, &block)
|
||||||
|
@ -90,10 +90,21 @@ module ActiveSupport
|
||||||
|
|
||||||
if block_given?
|
if block_given?
|
||||||
block.call
|
block.call
|
||||||
simple_stubs.unstub_all!
|
travel_back
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Return the current time back to its original state.
|
||||||
|
#
|
||||||
|
# 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
|
private
|
||||||
|
|
||||||
def simple_stubs
|
def simple_stubs
|
||||||
|
|
|
@ -201,4 +201,16 @@ class TimeHelperTest < ActiveSupport::TestCase
|
||||||
assert_not_equal expected_time, Time.now
|
assert_not_equal expected_time, Time.now
|
||||||
assert_not_equal Date.new(2004, 11, 24), Date.today
|
assert_not_equal Date.new(2004, 11, 24), Date.today
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -607,6 +607,9 @@ for detailed changes.
|
||||||
`Time.now` and
|
`Time.now` and
|
||||||
`Date.today`. ([Pull Request](https://github.com/rails/rails/pull/12824))
|
`Date.today`. ([Pull Request](https://github.com/rails/rails/pull/12824))
|
||||||
|
|
||||||
|
* Added `ActiveSupport::Testing::TimeHelpers#travel_back`. This method return
|
||||||
|
the current time to the original state. ([Pull Request](https://github.com/rails/rails/pull/13884))
|
||||||
|
|
||||||
* Added `Numeric#in_milliseconds`, like `1.hour.in_milliseconds`, so we can feed
|
* Added `Numeric#in_milliseconds`, like `1.hour.in_milliseconds`, so we can feed
|
||||||
them to JavaScript functions like
|
them to JavaScript functions like
|
||||||
`getTime()`. ([Commit](https://github.com/rails/rails/commit/423249504a2b468d7a273cbe6accf4f21cb0e643))
|
`getTime()`. ([Commit](https://github.com/rails/rails/commit/423249504a2b468d7a273cbe6accf4f21cb0e643))
|
||||||
|
|
Loading…
Reference in a new issue