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.