mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #44088 from KevSlashNull/KevSlashNull-patch-1
Allow subsecond resolution in `travel_to` helper
This commit is contained in:
commit
aa3a6c9c1b
2 changed files with 41 additions and 3 deletions
|
@ -115,7 +115,8 @@ module ActiveSupport
|
||||||
#
|
#
|
||||||
# Note that the usec for the time passed will be set to 0 to prevent rounding
|
# Note that the usec for the time passed will be set to 0 to prevent rounding
|
||||||
# errors with external services, like MySQL (which will round instead of floor,
|
# errors with external services, like MySQL (which will round instead of floor,
|
||||||
# leading to off-by-one-second errors).
|
# leading to off-by-one-second errors), unless the <tt>with_usec</tt> argument
|
||||||
|
# is set to <tt>true</tt>.
|
||||||
#
|
#
|
||||||
# This method also accepts a block, which will return the current time back to its original
|
# This method also accepts a block, which will return the current time back to its original
|
||||||
# state at the end of the block:
|
# state at the end of the block:
|
||||||
|
@ -125,7 +126,7 @@ module ActiveSupport
|
||||||
# Time.current # => 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)
|
def travel_to(date_or_time, with_usec: false)
|
||||||
if block_given? && in_block
|
if block_given? && in_block
|
||||||
travel_to_nested_block_call = <<~MSG
|
travel_to_nested_block_call = <<~MSG
|
||||||
|
|
||||||
|
@ -158,12 +159,14 @@ module ActiveSupport
|
||||||
now = date_or_time.midnight.to_time
|
now = date_or_time.midnight.to_time
|
||||||
elsif date_or_time.is_a?(String)
|
elsif date_or_time.is_a?(String)
|
||||||
now = Time.zone.parse(date_or_time)
|
now = Time.zone.parse(date_or_time)
|
||||||
|
elsif with_usec
|
||||||
|
now = date_or_time.to_time
|
||||||
else
|
else
|
||||||
now = date_or_time.to_time.change(usec: 0)
|
now = date_or_time.to_time.change(usec: 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
stubbed_time = Time.now if simple_stubs.stubbing(Time, :now)
|
stubbed_time = Time.now if simple_stubs.stubbing(Time, :now)
|
||||||
simple_stubs.stub_object(Time, :now) { at(now.to_i) }
|
simple_stubs.stub_object(Time, :now) { at(now.to_f) }
|
||||||
simple_stubs.stub_object(Date, :today) { jd(now.to_date.jd) }
|
simple_stubs.stub_object(Date, :today) { jd(now.to_date.jd) }
|
||||||
simple_stubs.stub_object(DateTime, :now) { jd(now.to_date.jd, now.hour, now.min, now.sec, Rational(now.utc_offset, 86400)) }
|
simple_stubs.stub_object(DateTime, :now) { jd(now.to_date.jd, now.hour, now.min, now.sec, Rational(now.utc_offset, 86400)) }
|
||||||
|
|
||||||
|
|
|
@ -186,6 +186,41 @@ class TimeTravelTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_time_helper_travel_to_with_usec
|
||||||
|
Time.stub(:now, Time.now) do
|
||||||
|
duration_usec = 0.1.seconds
|
||||||
|
traveled_time = Time.new(2004, 11, 24, 1, 4, 44) + duration_usec
|
||||||
|
expected_time = Time.new(2004, 11, 24, 1, 4, 44)
|
||||||
|
|
||||||
|
assert_nothing_raised do
|
||||||
|
travel_to traveled_time
|
||||||
|
|
||||||
|
assert_equal expected_time, Time.now
|
||||||
|
|
||||||
|
travel_back
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
travel_back
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_time_helper_travel_to_with_usec_true
|
||||||
|
Time.stub(:now, Time.now) do
|
||||||
|
duration_usec = 0.1.seconds
|
||||||
|
expected_time = Time.new(2004, 11, 24, 1, 4, 44) + duration_usec
|
||||||
|
|
||||||
|
assert_nothing_raised do
|
||||||
|
travel_to expected_time, with_usec: true
|
||||||
|
|
||||||
|
assert_equal expected_time.to_f, Time.now.to_f
|
||||||
|
|
||||||
|
travel_back
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
travel_back
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_time_helper_travel_with_subsequent_block
|
def test_time_helper_travel_with_subsequent_block
|
||||||
Time.stub(:now, Time.now) do
|
Time.stub(:now, Time.now) do
|
||||||
outer_expected_time = Time.new(2004, 11, 24, 1, 4, 44)
|
outer_expected_time = Time.new(2004, 11, 24, 1, 4, 44)
|
||||||
|
|
Loading…
Reference in a new issue