Skip to content

Commit a199cc6

Browse files
stefannibrasilcompwron
authored andcommitted
Build factories instead of creating as much as possible
This reduces test setup time. I also find it easier to understand when the test checks that an attribute is present instead of not nil.
1 parent d87e51e commit a199cc6

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

spec/system/devise/passwords/new_spec.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
end
88

99
describe "reset password page" do
10-
let!(:user) { create(:user, email: "glados@aperture.labs", phone_number: "+16578900012") }
11-
1210
it "displays error messages for non-existent user" do
13-
fill_in "Email", with: "tangerine@forward.com"
11+
user = build(:user, email: "glados@example.com", phone_number: "+16578900012")
12+
13+
fill_in "Email", with: "tangerine@example.com"
1414
fill_in "Phone number", with: user.phone_number
1515

1616
click_on "Send me reset password instructions"
1717
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."
1818
end
1919

2020
it "displays phone number error messages for incorrect formatting" do
21+
user = create(:user, email: "glados@example.com", phone_number: "+16578900012")
22+
2123
fill_in "Email", with: user.email
2224
fill_in "Phone number", with: "2134567eee"
2325

@@ -26,12 +28,16 @@
2628
expect(page).to have_text("Phone number must be 10 digits or 12 digits including country code (+1)")
2729
end
2830

29-
it "displays error if user tries to submit empty form" do
31+
it "displays error if user tries to submit an empty form" do
32+
user = build(:user, email: "glados@example.com", phone_number: "+16578900012")
33+
3034
click_on "Send me reset password instructions"
3135
expect(page).to have_text("Please enter at least one field.")
3236
end
3337

3438
it "redirects to sign up page for email" do
39+
user = build(:user, email: "glados@example.com", phone_number: "+16578900012")
40+
3541
fill_in "Email", with: user.email
3642

3743
click_on "Send me reset password instructions"
@@ -69,7 +75,7 @@
6975

7076
token = reset_password_link(user.email).gsub("http://localhost:3000/users/password/edit?reset_password_token=", "")
7177
encrypted_token = Devise.token_generator.digest(User, :reset_password_token, token)
72-
expect(User.find_by(reset_password_token: encrypted_token)).not_to be_nil
78+
expect(User.find_by(reset_password_token: encrypted_token)).to be_present
7379
end
7480

7581
it "user can update password" do

0 commit comments

Comments
 (0)