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.
This commit is contained in:
Javi Martín
2025-03-14 22:00:11 +01:00
parent 9a7681b75f
commit f5f96ba86e
3 changed files with 17 additions and 9 deletions

View File

@@ -26,8 +26,10 @@ shared_examples "notifiable in-app" do |factory_name|
end end
scenario "Multiple users commented on my notifiable" do scenario "Multiple users commented on my notifiable" do
3.times do |n| users = 3.times.map { create(:user, :verified) }
login_as(create(:user, :verified))
users.each.with_index do |user, n|
login_as(user)
visit path_for(notifiable) visit path_for(notifiable)
@@ -61,9 +63,10 @@ shared_examples "notifiable in-app" do |factory_name|
scenario "Multiple replies to my comment" do scenario "Multiple replies to my comment" do
comment = create(:comment, commentable: notifiable, user: author) comment = create(:comment, commentable: notifiable, user: author)
users = 3.times.map { create(:user, :verified) }
3.times do |n| users.each.with_index do |user, n|
login_as(create(:user, :verified)) login_as(user)
visit path_for(notifiable) visit path_for(notifiable)
within("#comment_#{comment.id}_reply") { click_link "Reply" } within("#comment_#{comment.id}_reply") { click_link "Reply" }

View File

@@ -74,6 +74,7 @@ describe "Voter" do
end end
scenario "Voting in booth" do scenario "Voting in booth" do
admin_user = admin.user
login_through_form_as_officer(officer) login_through_form_as_officer(officer)
visit new_officing_residence_path visit new_officing_residence_path
@@ -88,7 +89,7 @@ describe "Voter" do
end end
logout logout
login_as(admin.user) login_as(admin_user)
visit admin_poll_recounts_path(poll) visit admin_poll_recounts_path(poll)
within("#total_system") do within("#total_system") do
@@ -155,6 +156,7 @@ describe "Voter" do
end end
scenario "Trying to vote in booth and then in web" do scenario "Trying to vote in booth and then in web" do
admin_user = admin.user
login_through_form_as_officer(officer) login_through_form_as_officer(officer)
vote_for_poll_via_booth vote_for_poll_via_booth
@@ -170,7 +172,7 @@ describe "Voter" do
"You can not participate again." "You can not participate again."
logout logout
login_as(admin.user) login_as(admin_user)
visit admin_poll_recounts_path(poll) visit admin_poll_recounts_path(poll)
within("#total_system") do within("#total_system") do
@@ -186,6 +188,7 @@ describe "Voter" do
scenario "Voting in poll and then verifiying account" do scenario "Voting in poll and then verifiying account" do
allow_any_instance_of(Verification::Sms).to receive(:generate_confirmation_code).and_return("1357") allow_any_instance_of(Verification::Sms).to receive(:generate_confirmation_code).and_return("1357")
user = create(:user) user = create(:user)
admin_user = admin.user
login_through_form_as_officer(officer) login_through_form_as_officer(officer)
vote_for_poll_via_booth vote_for_poll_via_booth
@@ -208,7 +211,7 @@ describe "Voter" do
"You can not participate again." "You can not participate again."
logout logout
login_as(admin.user) login_as(admin_user)
visit admin_poll_recounts_path(poll) visit admin_poll_recounts_path(poll)
within("#total_system") do within("#total_system") do

View File

@@ -181,6 +181,7 @@ describe "Users" do
end end
scenario "is always visible for admins" do scenario "is always visible for admins" do
admin = create(:administrator).user
login_as(user) login_as(user)
visit account_path visit account_path
@@ -190,13 +191,14 @@ describe "Users" do
expect(page).to have_content "Changes saved" expect(page).to have_content "Changes saved"
logout logout
login_as(create(:administrator).user) login_as(admin)
visit user_path(user) visit user_path(user)
expect(page).not_to have_content "activity list private" expect(page).not_to have_content "activity list private"
end end
scenario "is always visible for moderators" do scenario "is always visible for moderators" do
moderator = create(:moderator).user
login_as(user) login_as(user)
visit account_path visit account_path
@@ -206,7 +208,7 @@ describe "Users" do
expect(page).to have_content "Changes saved" expect(page).to have_content "Changes saved"
logout logout
login_as(create(:moderator).user) login_as(moderator)
visit user_path(user) visit user_path(user)
expect(page).not_to have_content "activity list private" expect(page).not_to have_content "activity list private"