While RSpec is extremely popular in the Ruby community, I prefer to have less magic and better readability in my test suites. That means I use Minitest whenever I can.
There seems to be endless documentation for how to test Rails controller concerns with RSpec and little to none for Minitest so I decided to put together a quick writeup on how I’ve been going about it.
Let’s start with an example controller concern that sets a @current_user
instance variable:
Now let’s take a look at a simple test case for this module:
This test creates an ephemeral controller class that
includes our controller concern and an index method that simply renders a plain
text response. The test case calls that index method, asserts we got a 200 OK
reponse and the @current_user
assignment is an instance of the NullUser
class. That’s all there is to it!