Test authorization logic in Rails
23 April 2022 (Updated 29 April 2022)
Suppose you decide to implement authorization checks using a gem like Pundit. Pundit provides a nice DSL for writing policy specs, but it’s possible that even though you’ve written correct authorization policies, you forget to call them in your controller actions.
Pundit offers a built-in way of avoiding this mistake, but I personally find that method a little quirky and confusing. Instead, I prefer to include authorization checks as part of my feature tests. Something like this:
I like to use a raise_unauthorized_error
custom RSpec matcher to improve code readability:
Tagged:
Rails testing
Thanks for your comment . Once it's approved, it will appear here.
Leave a comment