From b787e33883bd917bcfb5887af54b5882e4698ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 30 Aug 2018 22:01:33 +0200 Subject: [PATCH] Use the same system to freeze time in all specs This required changing the `voted_before_sign_in` slightly in order to change what the method returns if the user signed in and voted at the exact same microsecond. It doesn't affect production code because it would be impossible for the user to do both things at the same time. As a side effect, the method now returns what the method name suggests. Before this change, the correct method name would have been `voted_before_or_at_the_same_time_of_sign_in`. As a less desirable side effect, in the tests now we need to make sure at least one second passes between the moment a user votes and the moment a user signs in again. One microsecond wouldn't work because the method `travel_to` automatically sets microseconds to zero in order to avoid rounding issues. --- app/helpers/polls_helper.rb | 2 +- spec/features/polls/polls_spec.rb | 10 ++-------- spec/features/polls/voter_spec.rb | 18 ++++++++++++------ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/helpers/polls_helper.rb b/app/helpers/polls_helper.rb index 0d5b0a605..0cb875dff 100644 --- a/app/helpers/polls_helper.rb +++ b/app/helpers/polls_helper.rb @@ -46,7 +46,7 @@ module PollsHelper end def voted_before_sign_in(question) - question.answers.where(author: current_user).any? { |vote| current_user.current_sign_in_at >= vote.updated_at } + question.answers.where(author: current_user).any? { |vote| current_user.current_sign_in_at > vote.updated_at } end end diff --git a/spec/features/polls/polls_spec.rb b/spec/features/polls/polls_spec.rb index a2e009f77..b95bb8129 100644 --- a/spec/features/polls/polls_spec.rb +++ b/spec/features/polls/polls_spec.rb @@ -387,18 +387,12 @@ feature 'Polls' do end end - context 'Booth & Website' do + context 'Booth & Website', :with_frozen_time do - let(:poll) { create(:poll, summary: "Summary", description: "Description", starts_at: '2017-12-01', ends_at: '2018-02-01') } + let(:poll) { create(:poll, summary: "Summary", description: "Description") } let(:booth) { create(:poll_booth) } let(:officer) { create(:poll_officer) } - before do - allow(Date).to receive(:current).and_return Date.new(2018,1,1) - allow(Date).to receive(:today).and_return Date.new(2018,1,1) - allow(Time).to receive(:current).and_return Time.zone.parse("2018-01-01 12:00:00") - end - scenario 'Already voted on booth cannot vote on website', :js do create(:poll_shift, officer: officer, booth: booth, date: Date.current, task: :vote_collection) diff --git a/spec/features/polls/voter_spec.rb b/spec/features/polls/voter_spec.rb index 5ba89fc3c..f7f1b58c8 100644 --- a/spec/features/polls/voter_spec.rb +++ b/spec/features/polls/voter_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' feature "Voter" do - context "Origin" do + context "Origin", :with_frozen_time do let(:poll) { create(:poll, :current) } let(:question) { create(:poll_question, poll: poll) } @@ -125,12 +125,18 @@ feature "Voter" do click_link "Sign out" - login_as user - visit poll_path(poll) + # Time needs to pass between the moment we vote and the moment + # we log in; otherwise the link to vote won't be available. + # It's safe to advance one second because this test isn't + # affected by possible date changes. + travel 1.second do + login_as user + visit poll_path(poll) - within("#poll_question_#{question.id}_answers") do - expect(page).to have_link(answer_yes.title) - expect(page).to have_link(answer_no.title) + within("#poll_question_#{question.id}_answers") do + expect(page).to have_link(answer_yes.title) + expect(page).to have_link(answer_no.title) + end end end end