2012-08-12 5 views
4

RSpec 및 rspec-rails 2.10으로 테스트하고 있습니다.RSpec 표준 시간대를 설정하는 방법은 무엇입니까?

environment.rb에서 Rails 표준 시간대를 UTC로 설정했지만 RSpec 표준 시간대는 현지 시간대 인 도쿄로 설정됩니다.

environments/test.rb 및 spec_helper.rb에서 Time.zone 설정을했지만 작성하지 않았습니다.

RSpec 표준 시간대를 설정하는 방법은 무엇입니까?

+0

'Time.zone'은 ActiveSupport :: TimeWithZone을 사용하는데 이는 레일스에서 ​​시간대를 설정하는 올바른 방법입니다. RSpec에서 ActiveSupport :: TimeWithZone 클래스를 사용하고 있습니까? 올바른 사용법은이 [documentation] (http://jsfiddle.net/ARf9M/1/)을 참조하십시오. –

+0

코드를 올리시겠습니까? "RSpec 표준 시간대"는 다소 모호합니다. –

+0

@TanzeebKhalili, 귀하가 올린 링크는 하이 차트의 자바 스크립트 피들이며 Rspec, Timezones 또는 ActiveSupport와는 관련이 없습니다. – gardenofwine

답변

0

문제는 다른 점입니다. 표준 시간대 오프셋을 조정하는 메서드를 작성했으며이 메서드는 Rails 표준 시간대를 변경합니다. 다음은 코드입니다.

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 
-1

사용 Time.zone.now 대신 Time.now.

+4

질문은 시간대를 변경하는 방법에 관한 것입니다. – pungoyal

관련 문제