This course came at the perfect time. I’ve recently gotten back into Rails after an 18-year hiatus, and this was a perfect refresher and shows just how much you can accomplish with Rails right out of the box.Garrett Winder
Beyond unit-level model tests, I believe it's essential to define controller-level tests that validate authentication and authorization logic, ensuring that everything behaves as expected.
When you scaffold resources in Rails, it creates two types of tests: system tests and controller tests. Personally, I find system tests, which use a headless browser, to be overkill for most applications. Instead, I prefer using controller tests—they are faster, simpler, and more efficient, though somewhat less flexible. For what we are building, however, they are a perfect fit.
The pattern I follow for controller tests ensures comprehensive test coverage across the application.
By structuring tests this way, I ensure that both types of user experiences are properly covered. Additionally, I make sure to test every RESTful action, even if some of them aren't explicitly defined for a given controller. This helps catch potential edge cases and improves confidence in application stability.
For unauthenticated users, I set up a test case for a comment controller and walk through each RESTful action:
For authenticated users, I run the same set of tests but with a logged-in user session:
By using a controller-level authentication helper, I can avoid headless browser testing, making the tests faster and more efficient.
After running my tests, I found a few failing assertions—something I always expect when tweaking scaffolded controllers.
Fixing User Controller Authorization:
Fixing Post Controller Issues:
Fixing Comments Controller Issues:
After making these changes, I re-ran all controller tests for: ✅ Users ✅ Posts ✅ Comments
Everything passed successfully!
Having controller tests in place is a crucial foundation for a production-ready Rails application. These tests:
With solid model and controller tests in place, we can now shift our focus to the view layer, making our application look as polished and user-friendly as possible.