Devise Reset Password Unhappy Path Tests and Refactoring
Channel: Stateless Code
Date: 2022-11-15

In our previous video, we expended a lot of energy to get the happy path of the reset password flow tests to work. In this video we implement the tests for our error scenarios, do some refactoring, and enhance our tests to make assertions about emails being delivered.

The implementation of our error scenarios is fairly straightforward, and the first round of refactoring goes fairly smoothly.

Our smooth sailing ends when we decide to try making assertions about delivered emails. ApplicationSystemTestCase does not include the methods from ActionMailer::TestHelper even though the Rails Testing Guide indicates that it would. Where you decide to place the assert_emails block also seems to be a bit temperamental. We also apply the email test to UserSignUpTest.

We take a quick look to see if we can eliminate the RuboCop disable pragma comments by making our test methods private. This works, but sends our UserSignUpTest over 100 lines. After a little trial and error, we're able to do a little more refactoring and get the cops off our tails.

This video covers:
00:00:12 Introduction
00:04:04 Overview of the reset password error scenarios
00:05:47 User cannot reset password unless email matches a user
00:07:25 User cannot reset password with an invalid token
00:08:49 Reset password errors out if password confirmation doesn't match
00:10:11 Reset password errors out if password too short
00:11:17 Refactor repeated test code into helper methods
00:12:42 Refactor out reusable helper method to parse token from email into test_helper
00:14:33 Review other helper methods and refactoring within the original test class and re-run tests. Fix RuboCop violations
00:18:12 Add assertions about the emails and troubleshoot why it's not working as expected
00:28:45 Add assertions about emails to UserSignUpTest 
00:34:22 Make helper methods private and remove RuboCop disable from the tests
00:37:10 Look for additional opportunities to refactor UserSignUpTest
00:45:10 Review git diff and add comments
00:46:29 One last refactor on UserSignUpTest
00:49:55 Commit, push, and update project

#ruby #rails7 #rails #rubyonrails #rubygems #codecast #screencast #github #opensource #minitest #TDD #agile #NerdDice #DnD #roleplaying #softwaredevelopment #learntocode #tailwind #postgresql #rubocop #devise #turbo #controllers #views #selenium #capybara #systemtest

See other related StatelessCode videos:
- Write and Debug Happy Path System Tests for Devise Reset Password https://youtu.be/yz4DJ2Rtsh0

Resources that we relied upon for this solution:
- Testing Rails Applications https://guides.rubyonrails.org/testing.html
- Rails API reference https://api.rubyonrails.org

This video is CC0 - No rights reserved. (YouTube doesn't allow this option when publishing.) All code is released under the UNLICENSE.  Stateless Code denies the concept of "intellectual property". Copying is not stealing.