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 guard and guard-minitest gems
to our Gemfile.

# Add to gemfile:group:testdogem'guard'gem'guard-minitest'end

Run bundle install.
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:

guard init minitest

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:

Hi, I’m Nithin Bekal.
I work at Shopify in Ottawa, Canada.
Previously, co-founder of
CrowdStudio.in and
WowMakers.
Ruby is my preferred programming language,
and the topic of most of my articles here,
but I'm also a big fan of Elixir.
Tweet to me at @nithinbekal.