Skip to content

Commit d87e51e

Browse files
stefannibrasilcompwron
authored andcommitted
Check what user sees first before checking DB assertions
When github's free CI servers are under heavy load, a race condition between the page loading and checking the database causes tests to flake. This is caused by a system test inputting data into a form then immediately checking the database without waiting for the form to finish submitting. For every database check in the system files, make sure it's preceded by a capybara matcher with automatic waiting or replace the database check with a check for something to appear on the page.
1 parent d5b052f commit d87e51e

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

spec/system/devise/passwords/new_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,28 @@
4545
it "sends user email" do
4646
fill_in "Email", with: user.email
4747

48-
expect { click_on "Send me reset password instructions" }.to change { ActionMailer::Base.deliveries.count }.by(1)
49-
expect(ActionMailer::Base.deliveries.last.to_addresses.map(&:address)).to include user.email
48+
click_on "Send me reset password instructions"
49+
50+
expect(page).to have_content "If the account exists you will receive an email or SMS with instructions on how to reset your password in a few minutes."
51+
52+
expect(ActionMailer::Base.deliveries.count).to eq(1)
53+
expect(ActionMailer::Base.deliveries.last.to).to eq([user.email])
5054
end
5155

5256
it "has reset password url with token" do
5357
fill_in "Email", with: user.email
5458
click_on "Send me reset password instructions"
5559

60+
expect(page).to have_content "If the account exists you will receive an email or SMS with instructions on how to reset your password in a few minutes."
5661
expect(reset_password_link(user.email)).to match(/http:\/\/localhost:3000\/users\/password\/edit\?reset_password_token=.*/)
5762
end
5863

5964
it "url token matches user's encrypted token" do
6065
fill_in "Email", with: user.email
6166
click_on "Send me reset password instructions"
6267

68+
expect(page).to have_content "If the account exists you will receive an email or SMS with instructions on how to reset your password in a few minutes."
69+
6370
token = reset_password_link(user.email).gsub("http://localhost:3000/users/password/edit?reset_password_token=", "")
6471
encrypted_token = Devise.token_generator.digest(User, :reset_password_token, token)
6572
expect(User.find_by(reset_password_token: encrypted_token)).not_to be_nil
@@ -80,6 +87,7 @@
8087
click_on "Log In"
8188

8289
expect(page).to have_text(user.display_name)
90+
expect(page).to have_text("My Cases")
8391
expect(page).not_to have_text("Sign in")
8492
end
8593
end

0 commit comments

Comments
 (0)