From f5f96ba86e863082a82ccb2b7e6c5df2c668c4dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 14 Mar 2025 22:00:11 +0100 Subject: [PATCH] Don't create records during a system test In the officing tests, we were accessing `admin.user` after starting the browser with a `visit`. However, since `admin` is a method with a `let` block, the administrator isn't created in the database until we referenced the variable, meaning we were creating the database record in the middle of the test. Referencing the variable at the beginning of the test solves the issue. We were also creating records to call the `login_as` method in the users tests, so we're moving the code to create them before the first call to `visit`. In the notifiable test, we were doing a loop consisting of "create()" -> "visit" -> "create()" -> "visit!" -> (...), meaning we were creating the second user after the first `visit`. Creating every user before the first `visit` solves the issue. --- spec/shared/system/notifiable_in_app.rb | 11 +++++++---- spec/system/polls/voter_spec.rb | 9 ++++++--- spec/system/users_spec.rb | 6 ++++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/spec/shared/system/notifiable_in_app.rb b/spec/shared/system/notifiable_in_app.rb index d8a59c86e..a768370e4 100644 --- a/spec/shared/system/notifiable_in_app.rb +++ b/spec/shared/system/notifiable_in_app.rb @@ -26,8 +26,10 @@ shared_examples "notifiable in-app" do |factory_name| end scenario "Multiple users commented on my notifiable" do - 3.times do |n| - login_as(create(:user, :verified)) + users = 3.times.map { create(:user, :verified) } + + users.each.with_index do |user, n| + login_as(user) visit path_for(notifiable) @@ -61,9 +63,10 @@ shared_examples "notifiable in-app" do |factory_name| scenario "Multiple replies to my comment" do comment = create(:comment, commentable: notifiable, user: author) + users = 3.times.map { create(:user, :verified) } - 3.times do |n| - login_as(create(:user, :verified)) + users.each.with_index do |user, n| + login_as(user) visit path_for(notifiable) within("#comment_#{comment.id}_reply") { click_link "Reply" } diff --git a/spec/system/polls/voter_spec.rb b/spec/system/polls/voter_spec.rb index 1c7991ff8..74dde4f60 100644 --- a/spec/system/polls/voter_spec.rb +++ b/spec/system/polls/voter_spec.rb @@ -74,6 +74,7 @@ describe "Voter" do end scenario "Voting in booth" do + admin_user = admin.user login_through_form_as_officer(officer) visit new_officing_residence_path @@ -88,7 +89,7 @@ describe "Voter" do end logout - login_as(admin.user) + login_as(admin_user) visit admin_poll_recounts_path(poll) within("#total_system") do @@ -155,6 +156,7 @@ describe "Voter" do end scenario "Trying to vote in booth and then in web" do + admin_user = admin.user login_through_form_as_officer(officer) vote_for_poll_via_booth @@ -170,7 +172,7 @@ describe "Voter" do "You can not participate again." logout - login_as(admin.user) + login_as(admin_user) visit admin_poll_recounts_path(poll) within("#total_system") do @@ -186,6 +188,7 @@ describe "Voter" do scenario "Voting in poll and then verifiying account" do allow_any_instance_of(Verification::Sms).to receive(:generate_confirmation_code).and_return("1357") user = create(:user) + admin_user = admin.user login_through_form_as_officer(officer) vote_for_poll_via_booth @@ -208,7 +211,7 @@ describe "Voter" do "You can not participate again." logout - login_as(admin.user) + login_as(admin_user) visit admin_poll_recounts_path(poll) within("#total_system") do diff --git a/spec/system/users_spec.rb b/spec/system/users_spec.rb index 204fa2673..17a42784d 100644 --- a/spec/system/users_spec.rb +++ b/spec/system/users_spec.rb @@ -181,6 +181,7 @@ describe "Users" do end scenario "is always visible for admins" do + admin = create(:administrator).user login_as(user) visit account_path @@ -190,13 +191,14 @@ describe "Users" do expect(page).to have_content "Changes saved" logout - login_as(create(:administrator).user) + login_as(admin) visit user_path(user) expect(page).not_to have_content "activity list private" end scenario "is always visible for moderators" do + moderator = create(:moderator).user login_as(user) visit account_path @@ -206,7 +208,7 @@ describe "Users" do expect(page).to have_content "Changes saved" logout - login_as(create(:moderator).user) + login_as(moderator) visit user_path(user) expect(page).not_to have_content "activity list private"