Gems for your Ruby on Rails application to make testing fun

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

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

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 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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
David Übelacker

Fullstack Developer & SoftwareArchitect | In love with Web & Mobile Applications