Rails Time Helpers part 2
In the first post about time helpers
I talked about
Believe it or not, it turns out you can also
And that turned out to be useful in a huge existing spec with a
in the before action and still allowed us to travel to a different date in the
specific scenario we were testing.
RSpec will get mad at you if you attempt at using a
travel_to or even a
block in the scenario or context otherwise.
The following example is not great in terms of content. It’s about the usage:
describe Foo do # ouch I know, it was already there! :/ before do travel_to(Date.new(2020, 10, 15)) end context "bar" do it "goes somewhere else" do # use unfreeze_time or travel_back travel_back user = FactoryBot.create(:user, name: "Foo") # then go 2 days in the future travel 2.days do user.update(name: "Bar") end expect(user.name).to eq("Bar") end end
Documentation on travel_back