Gems for your Ruby on Rails application to make testing fun

David Übelacker
4 min readFeb 4, 2022

--

20 years ago, when I started working as a programmer, we had our own test departments and teams that were only responsible for testing. There were hardly any automated tests. Everything was tested using textually described manual test cases.

In today’s agile world this is hardly imaginable, today automated tests are indispensable and writing tests is one of the tasks of every developer, ideally he even works test driven and writes the tests before he implements new features. So testing should make fun and can be fun!

Rails already provides a great testing experience, but the following gems make testing even more fun.

Guard & Guard Minitest

Guard automates various tasks by running custom rules whenever file or directories are modified.

It’s frequently used by software developers, web designers, writers and other specialists to avoid mundane, repetitive actions and commands such as “relaunching” tools after changing source files or configurations.(https://github.com/guard/guard)

If you want tests to run automatically in your console when you make changes to your code, then I can recommend the gems guard or guard-minitest.

According to your configuration, guard will automatically run your relevant tests as soon as you change something in your code. This way you don’t always have to run the tests manually.

Add this gems to your test secion in your gemfile:

gem 'guard'
gem 'guard-minitest'

and create a Guardfile in your project with follwoing content:

Minitest Reporters

Test output using minitest reporters

If you also want to have nice colorful output on the console and want to have nice html reports of your tests, then you can use minitest-reporters.

Add the gem to the test section of your Gemfile:

gem 'minitest-reporters'

and add following line to the bottom of your test/test_helper.rb file:

Minitest::Reporters.use! [Minitest::Reporters::SpecReporter.new, Minitest::Reporters::HtmlReporter.new]

FactoryBot & faker

One of the challenges in testing is to generate test data. This is usually tedious and not fun at all.

In this context FactoryBot brings super functionality to quickly and easily create instances of your models and faker is ideal to use random values in your tests.

Like always, just add both gems to test section of your Gemfile and run bundle install:

gem 'faker'
gem 'factory_bot'

Define factories for your models like the following in the directory test/factories:

Update your test_helper.rb as follows:

And then you can write very simple test like the following:

Shoulda

Shoulda Matchers provides RSpec- and Minitest-compatible one-liners to test common Rails functionality that, if written by hand, would be much longer, more complex, and error-prone.
(https://github.com/thoughtbot/shoulda-matchers)

Don’t want to repeat your self? then you need to add the shoulda gem to your Gemfile:

gem 'shoulda'

With shoulda you can test standard functionalities like associations or validations in a very simple way, here is an example:

Besides adding the gem to your Gemfile, you need to add following lines to your test_helper.rb:

Simplecov

SimpleCov is a code coverage analysis tool for Ruby. It uses Ruby’s built-in Coverage library to gather code coverage data, but makes processing its results much easier by providing a clean API to filter, group, merge, format, and display those results, giving you a complete code coverage suite that can be set up with just a couple lines of code.
https://github.com/simplecov-ruby/simplecov

Simplecov Report

I believe that for new projects you should always aim to have 100% test coverage. Of course, the goal of the tests should not only be a high test coverage, tests must also make sense. But with a 100% coverage strategy it is easier to see which parts of the code still need to be tested or you even notice that parts of your code are not needed at all.

It is better to explicitly exclude parts of the code that do not need to be tested than to have a lower test coverage then 100% in your report.

Best gem to get a test coverage report in ruby, ist the gem simplecov.

After adding that gem, just add following lines at the very top of your test_helper.rb:

require 'simplecov'
SimpleCov.start('rails')

and you will find a beautiful coverage report at coverage/index.html after every test run.

Finally

To see it all together in action, you can take a look at my Ruby on Rails playground repository: https://github.com/hackercowboy/rails-todo-playground

Link List

--

--

David Übelacker
David Übelacker

Written by David Übelacker

Fullstack Developer & Software Architect | In love with Web & Mobile Applications