Write System Tests for Devise Cancel My Account and Get Working With Turbo
Channel: Stateless Code
Date: 21/11/2022

Our journey through the epic of setting up and testing Devise continues with an often overlooked feature: a user cancelling an account. You may even ask why bother? Often businesses don't invest a lot into closing or deconversion of accounts. It's not a priority. Depending on what your terms and conditions and privacy policy are, a failure to have a working method of account destruction could cause you some trouble. We will resist the urge to launch into further discussion of data regulation here. It's enough to know that it's important that this functionality should work.

When we do the manual testing phase of this, we discover that the functionality is not working as intended. It is throwing an error, attempting to redirect to a users_url, which doesn't exist in our app.

In order to solve for this, we need to account for the registrations destroy scenario and redirect to our root path. It's a bit tricky finding the exact right data point because the methods defined and data available are different depending on which Devise controller you are using.

Eventually we're able to get it to work using the @controller instance variable. We then run our full suite of tests to ensure we didn't break anything else.

When we run RuboCop, we find that we are now in violation of the "Assignment Branch Condition" limit on complexity for a single method. We solve for this by refactoring out our controller recovery logic into a private method.

This video illustrates why you write automated tests. Even the articles and videos we used as guides for our previous video to get Devise working with Turbo did not account for this scenario.

This video covers:
00:00:12 Introduction
00:02:34 Demonstrate the functionality to be tested. Not working as intended.
00:04:30 Move registrations_edit_assertions! into ApplicationSystemTestCase as a public method
00:05:45 Create test class for user account cancellation and stub tests
00:06:59 Implement cancel registration test method with expectation that it will fail
00:09:06 Use test error message and dev environment page error to try and fix TurboDeviseController
00:12:57 Use debugger and puts statements to try to fix condition to use information that is available in the context of the method
00:24:19 Change root_url to /
00:25:10 Troubleshoot failure of assert_difference. Use assert_equal comparing before and after instead. 
00:29:08 Test passing. Run full suite to ensure
00:31:00 Implement test that user does not get destroyed when you dismiss the confirm
00:33:00 Run RuboCop and refactor TurboDeviseController to reduce Assignment Branch Condition size
00:36:41 Fix refactoring into wrong scope mistake 
00:38:18 Commit, push, update project, add comment to Devise issue with example

#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:
- Get Devise Working with Turbo in Rails 7 https://youtu.be/xnuA2K2mohs
- Fix Devise Views to Work with Tailwind and Turbo
https://youtu.be/8VjIML0Sdks

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.