From a8c467624076e5c03cf41e0fdbdd82556ba4b23e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 10 Apr 2021 20:14:52 +0200 Subject: [PATCH] Simplify system tests re-fetching records It's strange to create records without assigning them to a variable and then query the database to fetch the very same records. Assigning them to a variable makes the tests easier to understand. Besides, this way we avoid querying the database after the browser has started. --- spec/system/admin/budget_investments_spec.rb | 18 ++++++++---------- spec/system/admin/emails/newsletters_spec.rb | 4 ++-- spec/system/admin/signature_sheets_spec.rb | 4 ++-- spec/system/admin/stats_spec.rb | 4 ++-- spec/system/budgets/results_spec.rb | 5 +++-- 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/spec/system/admin/budget_investments_spec.rb b/spec/system/admin/budget_investments_spec.rb index e8a6ef6db..6315db41d 100644 --- a/spec/system/admin/budget_investments_spec.rb +++ b/spec/system/admin/budget_investments_spec.rb @@ -627,7 +627,7 @@ describe "Admin budget investments", :admin do administrator = create(:administrator, user: user) budget.administrators = [administrator] - create(:budget_investment, budget: budget, title: "Educate the children", + educate_children = create(:budget_investment, budget: budget, title: "Educate the children", administrator: administrator) create(:budget_investment, budget: budget, title: "More schools", administrator: administrator) @@ -648,8 +648,7 @@ describe "Admin budget investments", :admin do expect(page).to have_content("More schools") expect(page).not_to have_content("More hospitals") - educate_children_investment = Budget::Investment.find_by(title: "Educate the children") - fill_in "title_or_id", with: educate_children_investment.id + fill_in "title_or_id", with: educate_children.id click_button "Filter" expect(page).to have_css(".budget_investment", count: 1) @@ -661,7 +660,8 @@ describe "Admin budget investments", :admin do end scenario "Combination of select with text search" do - create(:budget_investment, :feasible, :finished, budget: budget, title: "Educate the children") + educate_children = create(:budget_investment, :feasible, :finished, + budget: budget, title: "Educate the children") create(:budget_investment, :feasible, :finished, budget: budget, title: "More schools") create(:budget_investment, budget: budget, title: "More hospitals") @@ -682,8 +682,7 @@ describe "Admin budget investments", :admin do expect(page).to have_content("More schools") expect(page).not_to have_content("More hospitals") - educate_children_investment = Budget::Investment.find_by(title: "Educate the children") - fill_in "title_or_id", with: educate_children_investment.id + fill_in "title_or_id", with: educate_children.id click_button "Filter" expect(page).to have_css(".budget_investment", count: 1) @@ -699,8 +698,8 @@ describe "Admin budget investments", :admin do administrator = create(:administrator, user: user) budget.administrators = [administrator] - create(:budget_investment, :feasible, :finished, budget: budget, title: "Educate the children", - administrator: administrator) + educate_children = create(:budget_investment, :feasible, :finished, + budget: budget, title: "Educate the children", administrator: administrator) create(:budget_investment, :feasible, :finished, budget: budget, title: "More schools", administrator: administrator) create(:budget_investment, budget: budget, title: "More hospitals", @@ -735,8 +734,7 @@ describe "Admin budget investments", :admin do expect(page).not_to have_content("More hospitals") expect(page).not_to have_content("More hostals") - educate_children_investment = Budget::Investment.find_by(title: "Educate the children") - fill_in "title_or_id", with: educate_children_investment.id + fill_in "title_or_id", with: educate_children.id click_button "Filter" expect(page).to have_css(".budget_investment", count: 1) diff --git a/spec/system/admin/emails/newsletters_spec.rb b/spec/system/admin/emails/newsletters_spec.rb index d58ddd8f1..5d2392395 100644 --- a/spec/system/admin/emails/newsletters_spec.rb +++ b/spec/system/admin/emails/newsletters_spec.rb @@ -33,13 +33,13 @@ describe "Admin newsletter emails", :admin do context "Index" do scenario "Valid newsletters" do - 3.times { create(:newsletter) } + newsletters = 3.times.map { create(:newsletter) } visit admin_newsletters_path expect(page).to have_css(".newsletter", count: 3) - Newsletter.find_each do |newsletter| + newsletters.each do |newsletter| segment_recipient = I18n.t("admin.segment_recipient.#{newsletter.segment_recipient}") within("#newsletter_#{newsletter.id}") do expect(page).to have_content newsletter.subject diff --git a/spec/system/admin/signature_sheets_spec.rb b/spec/system/admin/signature_sheets_spec.rb index 29fa6b0d5..032414f1a 100644 --- a/spec/system/admin/signature_sheets_spec.rb +++ b/spec/system/admin/signature_sheets_spec.rb @@ -3,13 +3,13 @@ require "rails_helper" describe "Signature sheets", :admin do context "Index" do scenario "Lists all signature_sheets" do - 3.times { create(:signature_sheet) } + signature_sheets = 3.times.map { create(:signature_sheet) } visit admin_signature_sheets_path expect(page).to have_css(".signature_sheet", count: 3) - SignatureSheet.find_each do |signature_sheet| + signature_sheets.each do |signature_sheet| expect(page).to have_content signature_sheet.name end end diff --git a/spec/system/admin/stats_spec.rb b/spec/system/admin/stats_spec.rb index 2c90ffbc6..49db027bc 100644 --- a/spec/system/admin/stats_spec.rb +++ b/spec/system/admin/stats_spec.rb @@ -255,14 +255,14 @@ describe "Stats", :admin do end scenario "Index" do - 3.times { create(:proposal_notification) } + proposal_notifications = 3.times.map { create(:proposal_notification) } visit admin_stats_path click_link "Proposal notifications" expect(page).to have_css(".proposal_notification", count: 3) - ProposalNotification.find_each do |proposal_notification| + proposal_notifications.each do |proposal_notification| expect(page).to have_content proposal_notification.title expect(page).to have_content proposal_notification.body end diff --git a/spec/system/budgets/results_spec.rb b/spec/system/budgets/results_spec.rb index 50d5c54b9..41d84fbf1 100644 --- a/spec/system/budgets/results_spec.rb +++ b/spec/system/budgets/results_spec.rb @@ -36,7 +36,7 @@ describe "Results" do visit budget_path(budget) click_link "See results" - expect(page).to have_selector("a.is-active", text: budget.headings.first.name) + expect(page).to have_selector("a.is-active", text: heading.name) within("#budget-investments-compatible") do expect(page).to have_content investment1.title @@ -108,7 +108,8 @@ describe "Results" do visit budget_path(budget) expect(page).not_to have_link "See results" - visit budget_results_path(budget, heading_id: budget.headings.first) + visit budget_results_path(budget, heading_id: heading) + expect(page).to have_content "You do not have permission to carry out the action" end