Rails tests: Setting up Guard with Minitest for quicker feedback
For doing test-driven development effectively, it is important to get instant feedback on your tests. For this, I like to configure my project to run the corresponding tests as soon as I save a file.
Guard is a useful tool that can watch for changes to files. Let’s look at how we can set up guard to run tests on a Rails project using Minitest testing framework.
First we add the
to our Gemfile.
Now we need to generate a Guardfile
which will define what to do
when a file gets changed.
To generate the Guardfile,
run this command:
This will generate a Guardfile configured to work with Minitest. This will also contain Rails-specific configuration that is commented out. Uncomment the Rails connfiguration and remove the Minitest::Unit configuration. The file should now have something like this:
Now you can run guard in the terminal and every time you change a file, the corresponding test file will be run.