Skip to content

Commit 65c8789

Browse files
ydahbbatsov
authored andcommitted
Add "Prefer freeze_time over travel_to with an argument of the current time" rule
Since there are several ways to indicate the current time, we believe that `freeze_time`, which can be expressed as simply as possible, should be preferred. ```ruby # bad travel_to(Time.now) travel_to(DateTime.now) travel_to(Time.current) travel_to(Time.zone.now) travel_to(Time.now.in_time_zone) travel_to(Time.current.to_time) # good freeze_time ```
1 parent 6bfbf8a commit 65c8789

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

README.adoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,24 @@ class MyControllerTest < ActionDispatch::IntegrationTest
17911791
end
17921792
----
17931793

1794+
=== `freeze_time` [[freeze-time]]
1795+
1796+
Prefer https://api.rubyonrails.org/classes/ActiveSupport/Testing/TimeHelpers.html#method-i-freeze_time[ActiveSupport::Testing::TimeHelpers#freeze_time] over https://api.rubyonrails.org/classes/ActiveSupport/Testing/TimeHelpers.html#method-i-travel_to[ActiveSupport::Testing::TimeHelpers#travel_to] with an argument of the current time.
1797+
1798+
[source,ruby]
1799+
----
1800+
# bad
1801+
travel_to(Time.now)
1802+
travel_to(DateTime.now)
1803+
travel_to(Time.current)
1804+
travel_to(Time.zone.now)
1805+
travel_to(Time.now.in_time_zone)
1806+
travel_to(Time.current.to_time)
1807+
1808+
# good
1809+
freeze_time
1810+
----
1811+
17941812
== Managing Processes
17951813

17961814
=== Foreman [[foreman]]

0 commit comments

Comments
 (0)