Skip to content

Commit 499e9dc

Browse files
serprexPhilip Dubé
authored andcommitted
TimeHelpers: include with_usec keyword parameter on travel & freeze too
1 parent 90a272a commit 499e9dc

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

activesupport/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* `ActiveSupport::Testing::TimeHelpers` now accepts named `with_usec` argument
2+
to `freeze_time`, `travel`, and `travel_to` methods. Passing true prevents
3+
truncating the destination time with `change(usec: 0)`.
4+
5+
*KevSlashNull*, and *serprex*
6+
17
* `ActiveSupport::CurrentAttributes.resets` now accepts a method name
28

39
The block API is still the recommended approach, but now both APIs are supported:

activesupport/lib/active_support/testing/time_helpers.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ def after_teardown
7575
# stubbing +Time.now+, +Date.today+, and +DateTime.now+. The stubs are automatically removed
7676
# at the end of the test.
7777
#
78+
# Note that the usec for the resulting time will be set to 0 to prevent rounding
79+
# errors with external services, like MySQL (which will round instead of floor,
80+
# leading to off-by-one-second errors), unless the <tt>with_usec</tt> argument
81+
# is set to <tt>true</tt>.
82+
#
7883
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
7984
# travel 1.day
8085
# Time.current # => Sun, 10 Nov 2013 15:34:49 EST -05:00
@@ -89,8 +94,8 @@ def after_teardown
8994
# User.create.created_at # => Sun, 10 Nov 2013 15:34:49 EST -05:00
9095
# end
9196
# Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
92-
def travel(duration, &block)
93-
travel_to Time.now + duration, &block
97+
def travel(duration, with_usec: false, &block)
98+
travel_to Time.now + duration, with_usec: with_usec, &block
9499
end
95100

96101
# Changes current time to the given time by stubbing +Time.now+,
@@ -217,7 +222,7 @@ def travel_back
217222
end
218223
alias_method :unfreeze_time, :travel_back
219224

220-
# Calls +travel_to+ with +Time.now+.
225+
# Calls +travel_to+ with +Time.now+. Forwards optional <tt>with_usec</tt> argument.
221226
#
222227
# Time.current # => Sun, 09 Jul 2017 15:34:49 EST -05:00
223228
# freeze_time
@@ -233,8 +238,8 @@ def travel_back
233238
# User.create.created_at # => Sun, 09 Jul 2017 15:34:49 EST -05:00
234239
# end
235240
# Time.current # => Sun, 09 Jul 2017 15:34:50 EST -05:00
236-
def freeze_time(&block)
237-
travel_to Time.now, &block
241+
def freeze_time(with_usec: false, &block)
242+
travel_to Time.now, with_usec: with_usec, &block
238243
end
239244

240245
private

activesupport/test/time_travel_test.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def test_time_helper_travel_to_with_usec
204204
end
205205
end
206206

207-
def test_time_helper_travel_to_with_usec_true
207+
def test_time_helper_with_usec_true
208208
Time.stub(:now, Time.now) do
209209
duration_usec = 0.1.seconds
210210
expected_time = Time.new(2004, 11, 24, 1, 4, 44) + duration_usec
@@ -214,13 +214,26 @@ def test_time_helper_travel_to_with_usec_true
214214

215215
assert_equal expected_time.to_f, Time.now.to_f
216216

217+
travel 0.5, with_usec: true
218+
219+
assert_equal((expected_time + 0.5).to_f, Time.now.to_f)
220+
217221
travel_back
218222
end
219223
ensure
220224
travel_back
221225
end
222226
end
223227

228+
def test_time_helper_freeze_time_with_usec_true
229+
# repeatedly test in case Time.now happened to actually be 0 usec
230+
assert 9.times.any? do
231+
freeze_time(with_usec: true) do
232+
Time.now.usec != 0
233+
end
234+
end
235+
end
236+
224237
def test_time_helper_travel_with_subsequent_block
225238
Time.stub(:now, Time.now) do
226239
outer_expected_time = Time.new(2004, 11, 24, 1, 4, 44)

0 commit comments

Comments
 (0)