From 19edf0466ba80e08ed9d59b14e09d3df56c18003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 28 Sep 2019 15:23:50 +0200 Subject: [PATCH] Simplify assigning booth to a voter Unless we're using the booth assignment for something else in the test, when creating a voter from booth, specifying the poll and the booth is enough to create a booth assignment. --- spec/factories/polls.rb | 4 +++- spec/models/poll/voter_spec.rb | 22 +++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/spec/factories/polls.rb b/spec/factories/polls.rb index 14a984e13..9514bbb56 100644 --- a/spec/factories/polls.rb +++ b/spec/factories/polls.rb @@ -202,8 +202,10 @@ FactoryBot.define do trait :from_booth do origin { "booth" } + transient { booth { association(:poll_booth) } } + booth_assignment do - association :poll_booth_assignment, poll: poll + association :poll_booth_assignment, poll: poll, booth: booth end officer_assignment do diff --git a/spec/models/poll/voter_spec.rb b/spec/models/poll/voter_spec.rb index 03a3befb4..55fa0c4ab 100644 --- a/spec/models/poll/voter_spec.rb +++ b/spec/models/poll/voter_spec.rb @@ -51,28 +51,24 @@ describe Poll::Voter do end it "is not valid if the user has already voted in different booth in the same poll" do - booth_assignment1 = create(:poll_booth_assignment, poll: poll) - booth_assignment2 = create(:poll_booth_assignment, poll: poll) - user = create(:user, :level_two) - voter1 = create(:poll_voter, user: user, poll: poll, booth_assignment: booth_assignment1) - voter2 = build(:poll_voter, user: user, poll: poll, booth_assignment: booth_assignment2) + create(:poll_voter, :from_booth, user: user, poll: poll, booth: create(:poll_booth)) - expect(voter2).not_to be_valid - expect(voter2.errors.messages[:document_number]).to eq(["User has already voted"]) + voter = build(:poll_voter, :from_booth, user: user, poll: poll, booth: booth) + + expect(voter).not_to be_valid + expect(voter.errors.messages[:document_number]).to eq(["User has already voted"]) end it "is valid if the user has already voted in the same booth in different poll" do - booth_assignment1 = create(:poll_booth_assignment, booth: booth) - booth_assignment2 = create(:poll_booth_assignment, booth: booth, poll: poll) - user = create(:user, :level_two) - voter1 = create(:poll_voter, user: user, booth_assignment: booth_assignment1) - voter2 = build(:poll_voter, user: user, booth_assignment: booth_assignment2) + create(:poll_voter, :from_booth, user: user, booth: booth, poll: create(:poll)) - expect(voter2).to be_valid + voter = build(:poll_voter, :from_booth, user: user, booth: booth, poll: poll) + + expect(voter).to be_valid end it "is not valid if the user has voted via web" do