I'm attempting to use the travel_to
method in ActiveSupport::Testing::TimeHelpers. I want to be able to use visit_page
as if it were another day. I can call Date.today
from within the block defined by the call to travel_to
, and it gives me the date I'm trying to test for. However, when Date.today
is called from within the controller of the page I'm visiting with capybara, it always gives me today's date.
Here's the test:
it 'shows the date' do
new_time = Time.local(2017, 11, 16, 10, 0, 0)
travel_to(new_time) do
puts Date.today
visit root_path
expect(page).to have_content '2017-11-16'
end
end
And the controller:
class RootPageController < ApplicationController
def index
@this_date = Date.today
end
end
And the test failure:
2017-11-16
...
Failure/Error: expect(page).to have_content '2017-11-16'
expected to find text "2017-11-16" in "2017-11-27"
I have a suspicion that somehow capybara causes the call to Date.today
in the controller to happen before my travel_to
block in the test. Is there a way to make sure that doesn't happen? Or, could something else be going on?