Timezone for RSpec tests
Don’t set Time.zone = 'Pacific Time (US & Canada)'
. It will change the Time
Zone for the rest of your tests.
Instead, you can implement a RSpec callback on that sets the timezone only for the specific test.
# spec/support/timezone.rb
config.around :example, :tz do |example|
Time.use_zone(example.metadata[:tz]) { example.run }
end
Then you can simply set the tz
metadata with the timezone you want to use on
your test.
it 'displays start time with user timezone', tz: 'Pacific Time (US & Canada)' do
user_timezone = 'Brasilia'
start_date = Time.zone.parse('2017-12-31 23:00:00')
event = Event.new(name: 'World Cup', start_date_time: start_date)
name_with_start_date = event.name_with_start_date(user_timezone)
expect(name_with_start_date).to eq 'World Cup (Jan 1, 2018)'
end
References:
Comments
comments powered by Disqus