Query the database before visiting a page in tests

We can assign query results to variables and so we avoid querying the
database after starting the browser.
This commit is contained in:
Javi Martín
2021-04-10 20:39:26 +02:00
parent 994745839b
commit a7664ad817
6 changed files with 31 additions and 22 deletions

View File

@@ -3,6 +3,7 @@ shared_examples "relationable" do |relationable_model_name|
let(:related1) { create([:proposal, :debate, :budget_investment].sample) } let(:related1) { create([:proposal, :debate, :budget_investment].sample) }
let(:related2) { create([:proposal, :debate, :budget_investment].sample) } let(:related2) { create([:proposal, :debate, :budget_investment].sample) }
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:url) { Setting["url"] }
scenario "related contents are listed" do scenario "related contents are listed" do
create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user)) create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
@@ -32,7 +33,7 @@ shared_examples "relationable" do |relationable_model_name|
click_on("Add related content") click_on("Add related content")
within("#related_content") do within("#related_content") do
fill_in "url", with: "#{Setting["url"] + related1.url}" fill_in "url", with: "#{url + related1.url}"
click_button "Add" click_button "Add"
end end
@@ -49,7 +50,7 @@ shared_examples "relationable" do |relationable_model_name|
click_on("Add related content") click_on("Add related content")
within("#related_content") do within("#related_content") do
fill_in "url", with: "#{Setting["url"] + related2.url}" fill_in "url", with: "#{url + related2.url}"
click_button "Add" click_button "Add"
end end
@@ -69,7 +70,7 @@ shared_examples "relationable" do |relationable_model_name|
click_button "Add" click_button "Add"
end end
expect(page).to have_content("Link not valid. Remember to start with #{Setting[:url]}.") expect(page).to have_content("Link not valid. Remember to start with #{url}.")
end end
scenario "returns error when relating content URL to itself" do scenario "returns error when relating content URL to itself" do
@@ -79,7 +80,7 @@ shared_examples "relationable" do |relationable_model_name|
click_on("Add related content") click_on("Add related content")
within("#related_content") do within("#related_content") do
fill_in "url", with: Setting[:url] + relationable.url.to_s fill_in "url", with: url + relationable.url.to_s
click_button "Add" click_button "Add"
end end

View File

@@ -17,12 +17,13 @@ describe "Documents", :admin do
3.times { create(:document, :admin) } 3.times { create(:document, :admin) }
1.times { create(:document) } 1.times { create(:document) }
document = Document.first
attachment = document.attachment
visit admin_site_customization_documents_path visit admin_site_customization_documents_path
expect(page).to have_content "There are 3 documents" expect(page).to have_content "There are 3 documents"
expect(page).to have_link document.title, href: attachment.url
document = Document.first
expect(page).to have_link document.title, href: document.attachment.url
end end
scenario "Index (empty)" do scenario "Index (empty)" do

View File

@@ -411,10 +411,12 @@ describe "Stats", :admin do
end end
scenario "Renders all goals stats" do scenario "Renders all goals stats" do
goals_count = SDG::Goal.count
visit sdg_admin_stats_path visit sdg_admin_stats_path
expect(page).to have_css "h3", count: SDG::Goal.count expect(page).to have_css "h3", count: goals_count
expect(page).to have_css ".sdg-goal-stats", count: SDG::Goal.count expect(page).to have_css ".sdg-goal-stats", count: goals_count
end end
end end
end end

View File

@@ -4,13 +4,12 @@ describe "Admin Budgets", :admin do
context "Index" do context "Index" do
scenario "Create poll if the budget does not have a poll associated" do scenario "Create poll if the budget does not have a poll associated" do
budget = create(:budget) budget = create(:budget)
balloting_phase = budget.phases.balloting
visit admin_budgets_path visit admin_budgets_path
click_link "Admin ballots" click_link "Admin ballots"
balloting_phase = budget.phases.find_by(kind: "balloting")
expect(page).to have_current_path(/admin\/polls\/\d+/) expect(page).to have_current_path(/admin\/polls\/\d+/)
expect(page).to have_content(budget.name) expect(page).to have_content(budget.name)
expect(page).to have_content(balloting_phase.starts_at.to_date) expect(page).to have_content(balloting_phase.starts_at.to_date)

View File

@@ -325,13 +325,14 @@ describe "Budget Investments" do
scenario "Random order maintained when going back from show" do scenario "Random order maintained when going back from show" do
per_page.times { create(:budget_investment, heading: heading) } per_page.times { create(:budget_investment, heading: heading) }
first_investment = Budget::Investment.first
visit budget_investments_path(budget, heading_id: heading.id) visit budget_investments_path(budget, heading_id: heading.id)
order = all(".budget-investment h3").map(&:text) order = all(".budget-investment h3").map(&:text)
expect(order).not_to be_empty expect(order).not_to be_empty
click_link Budget::Investment.first.title click_link first_investment.title
click_link "Go back" click_link "Go back"
new_order = all(".budget-investment h3").map(&:text) new_order = all(".budget-investment h3").map(&:text)
@@ -742,16 +743,17 @@ describe "Budget Investments" do
scenario "Price & explanation is shown when Budget is on published prices phase" do scenario "Price & explanation is shown when Budget is on published prices phase" do
Budget::Phase::PUBLISHED_PRICES_PHASES.each do |phase| Budget::Phase::PUBLISHED_PRICES_PHASES.each do |phase|
budget.update!(phase: phase) budget.update!(phase: phase)
if budget.finished?
investment.update!(winner: true)
end
visit budget_investment_path(budget, id: investment.id) visit budget_investment_path(budget, id: investment.id)
expect(page).to have_content(investment.formatted_price) expect(page).to have_content(investment.formatted_price)
expect(page).to have_content(investment.price_explanation) expect(page).to have_content(investment.price_explanation)
expect(page).to have_link("See price explanation") expect(page).to have_link("See price explanation")
if budget.finished?
investment.update(winner: true)
end
visit budget_investments_path(budget) visit budget_investments_path(budget)
expect(page).to have_content(investment.formatted_price) expect(page).to have_content(investment.formatted_price)

View File

@@ -125,16 +125,20 @@ describe "Officing Results", :with_frozen_time do
end end
scenario "Index lists all questions and answers" do scenario "Index lists all questions and answers" do
officer_assignment = poll_officer.officer_assignments.first
booth_assignment = officer_assignment.booth_assignment
booth = booth_assignment.booth
create(:poll_partial_result, create(:poll_partial_result,
officer_assignment: poll_officer.officer_assignments.first, officer_assignment: officer_assignment,
booth_assignment: poll_officer.officer_assignments.first.booth_assignment, booth_assignment: booth_assignment,
date: poll.ends_at, date: poll.ends_at,
question: question_1, question: question_1,
amount: 33) amount: 33)
create(:poll_recount, create(:poll_recount,
officer_assignment: poll_officer.officer_assignments.first, officer_assignment: officer_assignment,
booth_assignment: poll_officer.officer_assignments.first.booth_assignment, booth_assignment: booth_assignment,
date: poll.ends_at, date: poll.ends_at,
white_amount: 21, white_amount: 21,
null_amount: 44, null_amount: 44,
@@ -142,10 +146,10 @@ describe "Officing Results", :with_frozen_time do
visit officing_poll_results_path(poll, visit officing_poll_results_path(poll,
date: I18n.l(poll.ends_at.to_date), date: I18n.l(poll.ends_at.to_date),
booth_assignment_id: poll_officer.officer_assignments.first.booth_assignment_id) booth_assignment_id: officer_assignment.booth_assignment_id)
expect(page).to have_content(I18n.l(poll.ends_at.to_date, format: :long)) expect(page).to have_content(I18n.l(poll.ends_at.to_date, format: :long))
expect(page).to have_content(poll_officer.officer_assignments.first.booth_assignment.booth.name) expect(page).to have_content(booth.name)
expect(page).to have_content(question_1.title) expect(page).to have_content(question_1.title)
question_1.question_answers.each_with_index do |answer, i| question_1.question_answers.each_with_index do |answer, i|