From 92fc45cbb48ce379ae3b4fe388922ed503fd7072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 14 Mar 2025 04:10:28 +0100 Subject: [PATCH] Don't destroy records during a system test As mentioned in commits like a586ba806, a7664ad81, 006128da5, b41fbfa52 and c480cdd91, accessing the database after starting the browser with the `visit` method sometimes results in database corruption and failing tests on our CI due to the process running the test accessing the database after the process running the browser has started. IMHO this is also a bad practice for system tests, since these tests should be checking what users experience. In this case, however, I haven't found a way to destroy the VerifiedUser record using the interface, so we're stubbing `VerifiedUser` instead. Note we can't stub it since the beginning of the test because we're testing what happens when clicking the "Send code" button doesn't work, and we wouldn't be able to access the page containing the "Send code" if we stubbed `VerifiedUser` since the beginning of the test. --- spec/system/verification/email_spec.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/spec/system/verification/email_spec.rb b/spec/system/verification/email_spec.rb index 3bd429772..4cdc43b62 100644 --- a/spec/system/verification/email_spec.rb +++ b/spec/system/verification/email_spec.rb @@ -47,16 +47,18 @@ describe "Verify email" do document_number: "12345678Z", document_type: "dni") - verified_user = create(:verified_user, - document_number: "12345678Z", - document_type: "dni", - email: "rock@example.com") + create(:verified_user, + document_number: "12345678Z", + document_type: "dni", + email: "rock@example.com") login_as(user) - visit verified_user_path - verified_user.destroy! + expect(page).to have_button "Send code" + + allow(VerifiedUser).to receive(:by_user).and_return(VerifiedUser.none) + click_button "Send code" expect(page).to have_content "There was a problem with sending an email to your account"