Testing with time zones

I’m working on an app where builds are done on Travis. We have pages that display different data depending on whether you are looking at the “Yesterday” view or the “Today” view. We test some of these pages using RSpec feature specs.

Unfortunately, I can’t seem to write tests that work properly on both my development environment and on Travis. You are not supposed to use Timecop.freeze in Capybara tests. Some of the pages send AJAX calls to retrieve data, but they include a date as part of their query, and I think in the hours when Travis (UTC time) and my home (EST time) are on different calendar days that builds fail.

The tests pass on my local machine, and the pages work on Travis during most hours, and they also work in our staging environment on Heroku. Anyone have ideas as to how to handle this?

You can’t use freeze with js drivers because it screws up the capybara wait. But you can use travel. As long as your tests don’t require to the second precision that should be sufficient. Use the block format of travel so time is reset between specs.

Also, you should be setting your application’s time zone and being sure to use things like Date.current and Time.current vs date.today and Tine.now as the former pair will respect that time zone setting.

@joel gave a talk at a recent ruby meetup in Boston: http://bostonrb.org/presentations/timezones-there-here-then-now-and-back-again


That is exactly what I was looking for, @derekprior. Thanks very much, you’ve saved a couple more hours of headaches.

Excellent advice, @derekprior. I didn’t know that about the capybara limitations!