1

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?

  • 2
    It is not possible for the controller action to happen before you call `visit root_path` so that's not the issue. The issue is more likely to be you using a JS capable driver and not having puma (or whatever server is running the app) correctly configured to run in process. Are you using `puma` and if so what output does it create when you run your tests (if running rspec system tests you may need to un-silence the puma output). – Thomas Walpole Nov 27 '17 at 19:18
  • Hi, I'm not using a JS capable driver, just rack_test. I made sure there was no javascript so the issue wouldn't be confused by that. Maybe I'm making assumptions about how travel_to works. This seems to be the basic use case though. Have you used it that way? – Benjamin Blanchard Nov 28 '17 at 22:54

0 Answers0