A new helper to freeze time in Rails 5.2

Prem Sichanugrist
Sikachu's Blog
Published in
1 min readJun 8, 2018

--

This one is a simple quality-of-life improvement.

So, in Rails 5.2, instead of you doing:

travel_to(Time.current) do
# ...
end

You can actually use this new freeze_time helper:

freeze_time do
# ...
end

Internally, both of these methods are just mocking Time.current or Time.now to return given time, essentially “freezing the time”. I’m never a fan of travel_to(current_time) as I feel that it reads weird. I’m going to just use freeze_time from now on if the situation allows.

--

--

Senior Developer at Degica. I also contribute to open source projects, mostly in Ruby.