From 6c45d216264b6c01d1bc6127cb80abcf4e127076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 10 Oct 2019 02:34:23 +0200 Subject: [PATCH] Avoid using Time.now and Date.today in zone tests We've got a rubocop rule preventing us from using them, and the tests are easier to read this way. --- spec/models/poll/officer_spec.rb | 19 +++++++++---------- spec/spec_helper.rb | 7 +++++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/spec/models/poll/officer_spec.rb b/spec/models/poll/officer_spec.rb index 8c9e9525c..b8c8a29a3 100644 --- a/spec/models/poll/officer_spec.rb +++ b/spec/models/poll/officer_spec.rb @@ -124,20 +124,19 @@ describe Poll::Officer do end end - describe "todays_booths" do + describe "todays_booths", :application_zone_west_of_system_zone do let(:officer) { create(:poll_officer) } - it "returns booths for the application's time zone date", :application_zone_west_of_system_zone do - assignment_with_local_time_zone = create(:poll_officer_assignment, - date: Date.today, - officer: officer) + it "returns today's booths" do + todays_assignment = create(:poll_officer_assignment, date: Date.current, officer: officer) + expect(officer.todays_booths).to eq [todays_assignment.booth] + end - assignment_with_application_time_zone = create(:poll_officer_assignment, - date: Date.current, - officer: officer) + it "does not return booths for different dates" do + create(:poll_officer_assignment, date: Date.yesterday, officer: officer) + create(:poll_officer_assignment, date: Date.tomorrow, officer: officer) - expect(officer.todays_booths).to eq [assignment_with_application_time_zone.booth] - expect(officer.todays_booths).not_to include(assignment_with_local_time_zone.booth) + expect(officer.todays_booths).to be_empty end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ad465ecc5..d7ffa999b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -114,8 +114,11 @@ RSpec.configure do |config| system_zone = ActiveSupport::TimeZone.new("Madrid") allow(Time).to receive(:zone).and_return(application_zone) - allow(Time).to receive(:now).and_return(Date.current.end_of_day.in_time_zone(system_zone)) - allow(Date).to receive(:today).and_return(Time.now.to_date) + + system_time_at_application_end_of_day = Date.current.end_of_day.in_time_zone(system_zone) + + allow(Time).to receive(:now).and_return(system_time_at_application_end_of_day) + allow(Date).to receive(:today).and_return(system_time_at_application_end_of_day.to_date) end config.before(:each, :with_non_utc_time_zone) do