Hi guys…
How do you prevent Time issues due to daylight savings in Rspec?
The time zone in the specs are UTC
But in our application controller I am setting the time zone to User’s timezone which could be EST like this:
Time.zone = current_user.preferences.timezone
These would be affected by daylight savings time and specs randomly fail for a week due to this issue.
Is there a better workaround than doing this:
it "resends the invitations" do
Time.use_zone(user.preferences.timezone) do
invitation = create :invitation, user: user.preferences
now = Time.current
Timecop.freeze(now) do
post :invite, id: user.id
end
expect(invitation.reload.sent_at.to_i).to eq(now.to_i)
end
end
The other solution that i could come up with was to mock the timezone like:
it "resends the invitations" do
Time.use_zone(user.preferences.timezone) do
allow(Time).to receive(:zone) { Time.zone } #this would ensure all zones are UTC
....
end
end
In my app I am doing time comparaison like:
def can_reinvite?
return true if sent_at.nil?
self.sent_at < 1.week.ago
end