From 2ada826f12e1bf6decc19370a3b0f9f70221fc09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 13 Mar 2025 13:01:40 +0100 Subject: [PATCH] Move officing voters system test to controller test As mentioned in the previous commit, checking the database after starting the browser with the `visit` method sometimes results in database corruption and failing tests on our CI due to the process running the test accessing the database after the process running the browser has started. IMHO this is also a bad practice for system tests, since these tests should be checking what users experience. In this case, however, I haven't been able to test the user experience since it looks like booths and officer assignments for voters aren't shown anywhere. So, since the purpose of the test was to check the database, and there are other tests checking what happens after clicking the "Confirm vote" button in the user interface, we're converting this test into a controller test. --- .../officing/voters_controller_spec.rb | 39 ++++++++++++++++++ spec/system/officing/voters_spec.rb | 40 ------------------- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/spec/controllers/officing/voters_controller_spec.rb b/spec/controllers/officing/voters_controller_spec.rb index 146e230c7..4763615e4 100644 --- a/spec/controllers/officing/voters_controller_spec.rb +++ b/spec/controllers/officing/voters_controller_spec.rb @@ -22,5 +22,44 @@ describe Officing::VotersController do expect(Poll::Voter.count).to eq 1 expect(Poll::Voter.last.officer_id).to eq(officer.id) end + + it "stores officer and booth information" do + officer = create(:poll_officer) + user = create(:user, :in_census) + poll1 = create(:poll, name: "Would you be interested in XYZ?") + poll2 = create(:poll, name: "Testing polls") + + booth = create(:poll_booth) + + assignment1 = create(:poll_booth_assignment, poll: poll1, booth: booth) + assignment2 = create(:poll_booth_assignment, poll: poll2, booth: booth) + create(:poll_shift, officer: officer, booth: booth, date: Date.current, task: :vote_collection) + + validate_officer + set_officing_booth(booth) + sign_in(officer.user) + + post :create, params: { + voter: { poll_id: poll1.id, user_id: user.id }, + format: :js + } + expect(response).to be_successful + + post :create, params: { + voter: { poll_id: poll2.id, user_id: user.id }, + format: :js + } + expect(response).to be_successful + + expect(Poll::Voter.count).to eq(2) + + voter1 = Poll::Voter.first + expect(voter1.booth_assignment).to eq(assignment1) + expect(voter1.officer_assignment).to eq(assignment1.officer_assignments.first) + + voter2 = Poll::Voter.last + expect(voter2.booth_assignment).to eq(assignment2) + expect(voter2.officer_assignment).to eq(assignment2.officer_assignments.first) + end end end diff --git a/spec/system/officing/voters_spec.rb b/spec/system/officing/voters_spec.rb index 23c780a6f..ff26448a1 100644 --- a/spec/system/officing/voters_spec.rb +++ b/spec/system/officing/voters_spec.rb @@ -158,44 +158,4 @@ describe "Voters" do expect(page).not_to have_content poll1.name end end - - scenario "Store officer and booth information" do - create(:user, :in_census) - poll1 = create(:poll, name: "¿Quieres que XYZ sea aprobado?") - poll2 = create(:poll, name: "Pregunta de votación de prueba") - - second_booth = create(:poll_booth) - - ba1 = create(:poll_booth_assignment, poll: poll1, booth: second_booth) - ba2 = create(:poll_booth_assignment, poll: poll2, booth: second_booth) - create(:poll_shift, officer: officer, booth: second_booth, date: Date.current, task: :vote_collection) - - validate_officer - visit new_officing_residence_path - set_officing_booth(second_booth) - officing_verify_residence - - within("#poll_#{poll1.id}") do - click_button "Confirm vote" - - expect(page).to have_content "Vote introduced!" - end - - within("#poll_#{poll2.id}") do - click_button "Confirm vote" - - expect(page).to have_content "Vote introduced!" - end - - expect(Poll::Voter.count).to eq(2) - - voter1 = Poll::Voter.first - - expect(voter1.booth_assignment).to eq(ba1) - expect(voter1.officer_assignment).to eq(ba1.officer_assignments.first) - - voter2 = Poll::Voter.last - expect(voter2.booth_assignment).to eq(ba2) - expect(voter2.officer_assignment).to eq(ba2.officer_assignments.first) - end end