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.
This commit is contained in:
Javi Martín
2021-04-10 20:14:52 +02:00
parent fc32c767dd
commit a8c4676240
5 changed files with 17 additions and 18 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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