Fix rounding errors with #travel_to by resetting the usec on any passed time...

Fix rounding errors with #travel_to by resetting the usec on any passed time to zero, so we only travel with per-second precision, not anything deeper than that.
上级 88d27ae9
* Fix rounding errors with #travel_to by resetting the usec on any passed time to zero, so we only travel
with per-second precision, not anything deeper than that.
*DHH*
* Fix ActiveSupport::TestCase not to order users' test cases by default.
If this change breaks your tests because your tests are order dependent, you need to explicitly call
ActiveSupport::TestCase.my_tests_are_order_dependent! at the top of your tests.
......
......@@ -78,6 +78,10 @@ def travel(duration, &block)
# or <tt>Date.today</tt>, in order to honor the application time zone
# please always use <tt>Time.current</tt> and <tt>Date.current</tt>.)
#
# 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,
# leading to off-by-one-second errors).
#
# This method also accepts a block, which will return the current time back to its original
# state at the end of the block:
#
......@@ -90,7 +94,7 @@ def travel_to(date_or_time, &block)
if date_or_time.is_a?(Date) && !date_or_time.is_a?(DateTime)
now = date_or_time.midnight.to_time
else
now = date_or_time.to_time
now = date_or_time.to_time.change(usec: 0)
end
simple_stubs.stub_object(Time, :now, now)
......
......@@ -188,7 +188,7 @@ def test_time_helper_travel
expected_time = Time.now + 1.day
travel 1.day
assert_equal expected_time, Time.now
assert_equal expected_time.to_s(:db), Time.now.to_s(:db)
assert_equal expected_time.to_date, Date.today
end
......@@ -196,11 +196,11 @@ def test_time_helper_travel_with_block
expected_time = Time.now + 1.day
travel 1.day do
assert_equal expected_time, Time.now
assert_equal expected_time.to_s(:db), Time.now.to_s(:db)
assert_equal expected_time.to_date, Date.today
end
assert_not_equal expected_time, Time.now
assert_not_equal expected_time.to_s(:db), Time.now.to_s(:db)
assert_not_equal expected_time.to_date, Date.today
end
......@@ -235,4 +235,11 @@ def test_time_helper_travel_back
assert_not_equal expected_time, Time.now
assert_not_equal Date.new(2004, 11, 24), Date.today
end
def test_travel_to_will_reset_the_usec_to_avoid_mysql_rouding
travel_to Time.utc(2014, 10, 10, 10, 10, 50, 999999) do
assert_equal 50, Time.now.sec
assert_equal 0, Time.now.usec
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册