How to set RSpec time zone?

I am testing RSpec and rspec-rails 2.10.

I set the Rails timezone to UTC in environment.rb, but the RSpec timezone becomes my local timezone in Tokyo.

I wrote Time.zone setting in the /test.rb and spec_helper.rb environments, but didn't fix it.

How to set RSpec time zone?

+4
source share
2 answers

The problem is another point. I wrote a method that corrects the time zone offset, and the method changes the time zone of Rails. Here is the code.

class WeeklyEvent < ActiveRecord::Base def adjust_time_zone_offset Time.zone = timezone # => This is the problem. Rails Time.zone changes to timezone. time_zone_offset = Time.zone.utc_offset # => 32400 JST offset 9 hours in seconds. self.start_date_time = (start_date_time - time_zone_offset).in_time_zone('UTC') end end japanese_event = WeeklyEvent.find(1) p japanese_event.start_date_time #=> Sun, 01 Jan 2012 09:00:00 JST +09:00 
0
source

Use Time.zone.now instead of Time.now .

-1
source

All Articles