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
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)
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:
and create a Guardfile in your project with follwoing content:
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:
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.
Like always, just add both gems to test section of your Gemfile and run bundle install:
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 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.
Don’t want to repeat your self? then you need to add the shoulda gem to your Gemfile:
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 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.
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:
and you will find a beautiful coverage report at coverage/index.html after every test run.
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