Ruby on Rails — Testing and code quality (part 4)

Tools to test what you’ve been developing and help you keep your code simple and clean.

Hello my dears, are you tired of reading our blog posts about Ruby on Rails? Well, I’m going to be the bearer of bad news and I’ve come to torment you with another one. 😁

If you didn’t have the opportunity to see our previous articles about RoR please feel free to check them (Part 1, Part 2, Part 3). For this part, we’ll talk about some tools that Rails provides us in order to keep our code reliable, more clean and more awesome. 😎


Testing and code quality

Ok, so I’m a fantastic developer and I want my code to be the very best like no one ever was. For that, I want to test what I’ve been doing and also have clean and simple code. Let’s check bellow which gems can help us achieving this.

Unit testing

When it comes to unit testing, the more popular ones are MiniTest and RSpec. The first one is now included in Rails as the default framework for tests, so we don’t have to set up anything. However, a lot of people still prefer to work with RSpec, because not only it is compatible with all the other test-related gems, but it also lets us to have tests more readable due to being a Domain Specific Language. Furthermore, on these days, RSpec has a lot of documentation and examples through the web.

Testing utilities

Factory Bot, previously named Factory Girl, is a library that comes to our assistance when we arrive at a point where we need our models filled with dummy data, in order to run our automated tests.

Faker is used to seed random data in our application. This gem comes as a great help when we are building our test suite, because instead of having to write our data for our test resources (in our factories for example), we could just call Faker to generate a name, a date or mobile number and use it. Also, since it generates random data, we can run our tests with a mix of different data examples, and who doesn’t like to have strings filled with ‘Lorem ipsum’ and hipster names? 🤓

Database Cleaner is a tool that makes sure that every time that we run our automated tests suite, the database is cleared after each test. With this, we make sure that our tests run in an empty database, avoiding conflicts such as having wrongly duplicated data.

Shoulda Matchers comes to our aid with a bunch of helpers that let us assert a lot of things in our tests, for example, when we are testing a model and we want to test their validations and relations between them.

Webmock is an awesome library that makes it possible to test the integration of external services in our app. With this, we can stub HTTP requests and validate how our app reacts to different responses from the external services we are using.

Code quality

Regarding code quality, RoR has a lot of code analysers that can be used to do a lot of different types of checks, from code style to security issues and plenty others. Let’s talk about some of them:

RuboCop is probably the most popular analyser in the Ruby community and can maintain your code consistent and clean across the project, since it follows the community’s Ruby style guide. This gem is often found to be rage-inducing for plenty of developers. 🤬 It also allows us to define a list of rules (or ‘Cops’ per the documentation) for our ruby project in order to check for code style issues, metric issues (limit methods, models, controllers lines) and it can also check and automatically fix issues that we have like line breaks, gaps, bad practices. This is all reported on the console when we run it, and in some cases it even gives us refactoring suggestions.

Reek is an analyser that focuses more on checking for code smells that could lead to a lot of refactors. For example, methods with too many parameters, lots of repeated code that can be extracted to a helper method, methods with too many statements, among other features.

Brakeman is a gem that we should use when we want to detect vulnerabilities in our code. When we run this badass, we get a report with security issues that were found in the app. This is quite useful when we want to find issues like SQL Injection, format validations with regular expressions, unsafe redirects and plenty of other vulnerabilities that we won’t enumerate here. 😱

Flay is a tool that puts its efforts on searching our code for structural similarities. This way, we can prevent a lot of duplicated code in our app.

Rails Best Practices is similar to RuboCop, however it is not only focused on the Ruby language, but also on Rails code. For example, it will report if you have an unreachable route, if we didn’t add an index when creating foreign keys on the database, and plenty other common shortcomings.


WOW, finally we finished this boring blogpost right? After all, who wants to waste their time testing their application while guaranteeing a clean and organised codebase? Just let it rip…

So, I hope that this list of tools can help you improve your RoR applications. If you don’t agree with something or if you have more solutions that fit in this post, feel free to leave a comment because that will surely be helpful for my work and improve this blog post. Just to finish, I have to say that I have heard some rumours that we’ll have a part 5 😱. I don’t know who will write it but I’m already giving you my condolences if you are an avid follower.

It was a pleasure 🙌.

P.S. I don’t like minions, so sorry not sorry for that. Maybe Paulo can come back and bring those bastards again. Bye! 👋


Deemaze Software is a digital agency developing products for web and mobile. We do a lot more than sharing awesome Rails tools, so follow our work on Twitter, Facebook and Instagram.

Nice to meet you! 😉