From b431273869680c4a30e2efe8b2582beb6c04c058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 27 Sep 2019 16:12:02 +0200 Subject: [PATCH] Simplify creating officer assignments in specs Note we usually cannot make it simple because officer assignments are usually assigned to both a poll and a booth, and on a certain date. However, in the few cases where the booth nor the date don't matter, we can make the code a bit easier to read. --- spec/factories/polls.rb | 8 ++++++++ spec/factories/users.rb | 7 +++++++ .../admin/poll/booth_assigments_spec.rb | 4 +--- .../admin/poll/officer_assignments_spec.rb | 18 ++++++------------ spec/features/officing/results_spec.rb | 8 ++------ spec/models/poll/officer_spec.rb | 10 +++------- 6 files changed, 27 insertions(+), 28 deletions(-) diff --git a/spec/factories/polls.rb b/spec/factories/polls.rb index ca3c10649..17047c353 100644 --- a/spec/factories/polls.rb +++ b/spec/factories/polls.rb @@ -42,6 +42,14 @@ FactoryBot.define do trait :with_image do after(:create) { |poll| create(:image, imageable: poll) } end + + transient { officers { [] } } + + after(:create) do |poll, evaluator| + evaluator.officers.each do |officer| + create(:poll_officer_assignment, poll: poll, officer: officer) + end + end end factory :poll_question, class: "Poll::Question" do diff --git a/spec/factories/users.rb b/spec/factories/users.rb index b41c2ab5a..069a2cda0 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -115,6 +115,13 @@ FactoryBot.define do transient do sequence(:name) { |n| "Officer #{n}" } + polls { [] } + end + + after(:create) do |officer, evaluator| + evaluator.polls.each do |poll| + create(:poll_officer_assignment, poll: poll, officer: officer) + end end end diff --git a/spec/features/admin/poll/booth_assigments_spec.rb b/spec/features/admin/poll/booth_assigments_spec.rb index 0441363fb..b70e84da2 100644 --- a/spec/features/admin/poll/booth_assigments_spec.rb +++ b/spec/features/admin/poll/booth_assigments_spec.rb @@ -157,9 +157,7 @@ describe "Admin booths assignments" do booth = create(:poll_booth) officer_assignment = create(:poll_officer_assignment, poll: poll, booth: booth) officer = officer_assignment.officer - - officer_assignment_2 = create(:poll_officer_assignment, poll: poll) - officer_2 = officer_assignment_2.officer + officer_2 = create(:poll_officer, polls: [poll]) visit admin_poll_path(poll) click_link "Booths (2)" diff --git a/spec/features/admin/poll/officer_assignments_spec.rb b/spec/features/admin/poll/officer_assignments_spec.rb index 81c64a7ba..b2735bcae 100644 --- a/spec/features/admin/poll/officer_assignments_spec.rb +++ b/spec/features/admin/poll/officer_assignments_spec.rb @@ -10,12 +10,9 @@ describe "Officer Assignments" do scenario "Index" do poll = create(:poll) - officer1 = create(:poll_officer, name: "Bubbles") - officer2 = create(:poll_officer, name: "Blossom") - officer3 = create(:poll_officer, name: "Buttercup") - - officer_assignment = create(:poll_officer_assignment, poll: poll, officer: officer1) - officer_assignment_2 = create(:poll_officer_assignment, poll: poll, officer: officer2) + create(:poll_officer, name: "Bubbles", polls: [poll]) + create(:poll_officer, name: "Blossom", polls: [poll]) + create(:poll_officer, name: "Buttercup") visit admin_poll_path(poll) @@ -31,12 +28,9 @@ describe "Officer Assignments" do scenario "Search", :js do poll = create(:poll) - officer1 = create(:poll_officer, name: "John Snow") - officer2 = create(:poll_officer, name: "John Silver") - officer3 = create(:poll_officer, name: "John Edwards") - - officer_assignment = create(:poll_officer_assignment, poll: poll, officer: officer1) - officer_assignment_2 = create(:poll_officer_assignment, poll: poll, officer: officer2) + create(:poll_officer, name: "John Snow", polls: [poll]) + create(:poll_officer, name: "John Silver", polls: [poll]) + create(:poll_officer, name: "John Edwards") visit admin_poll_path(poll) diff --git a/spec/features/officing/results_spec.rb b/spec/features/officing/results_spec.rb index e73f5e41f..6bbeb9058 100644 --- a/spec/features/officing/results_spec.rb +++ b/spec/features/officing/results_spec.rb @@ -19,13 +19,9 @@ describe "Officing Results", :with_frozen_time do end scenario "Only polls where user is officer for results are accessible" do - regular_officer_assignment_1 = create(:poll_officer_assignment, officer: poll_officer) - regular_officer_assignment_2 = create(:poll_officer_assignment, officer: poll_officer) - not_allowed_poll_1 = create(:poll, :expired) - not_allowed_poll_2 = regular_officer_assignment_1.booth_assignment.poll - not_allowed_poll_2.update(ends_at: 1.day.ago) - not_allowed_poll_3 = regular_officer_assignment_2.booth_assignment.poll + not_allowed_poll_2 = create(:poll, officers: [poll_officer], ends_at: 1.day.ago) + not_allowed_poll_3 = create(:poll, officers: [poll_officer]) visit root_path click_link "Polling officers" diff --git a/spec/models/poll/officer_spec.rb b/spec/models/poll/officer_spec.rb index f494ed84e..cf7830935 100644 --- a/spec/models/poll/officer_spec.rb +++ b/spec/models/poll/officer_spec.rb @@ -65,13 +65,9 @@ describe Poll::Officer do it "returns polls ordered by end date (desc)" do officer = create(:poll_officer) - poll_1 = create(:poll, ends_at: 1.day.ago) - poll_2 = create(:poll, ends_at: 10.days.from_now) - poll_3 = create(:poll, ends_at: 10.days.ago) - - [poll_1, poll_2, poll_3].each do |poll| - create(:poll_officer_assignment, officer: officer, poll: poll) - end + poll_1 = create(:poll, ends_at: 1.day.ago, officers: [officer]) + poll_2 = create(:poll, ends_at: 10.days.from_now, officers: [officer]) + poll_3 = create(:poll, ends_at: 10.days.ago, officers: [officer]) assigned_polls = officer.voting_days_assigned_polls