close
Domain Expertise

Testing with Ruby on Rails – A Beginner’s Guide

Three months ago, I forayed into the world of Testing with Ruby on Rails. Off the bat, I was completely overwhelmed. Gems? Bundler? Rake? Rspec?

But as I began to wrap my head around it and overcome that initial speed bump, it became evident to me that I was being incredibly productive. I was getting work done faster than ever before. This is the magic of RoR. The entire platform is geared towards productivity and getting shit done.

Convention over ConfigurationIt enables one to use pre-created methods for performing standard tasks. It treats these methods as ‘black boxes’. Thus eliminating the need to understand their internal working.

I discovered that RoR has an immense online community. People are always providing support, upgrading redundant features and creating new Gems. Gems are plugins that one installs to perform certain tasks(like Add-ons for Chrome/Firefox). They range from standard – sending mails, setting up a web server, testing – to all kinds of crazy ones. There even exists a gem to manage other installed gems!

As a part of Team Jobspire, I handle all testing-related duties. This includes ensuring that the application would function as intended. That it would be bug-free and that it wouldn’t crash.

I conducted tests on two granularities – Unit Tests and Integration Tests.

Unit tests are low-level tests which verify the functionality of the individual classes. Say, verifying that an Age attribute of a Student model rejects strings and only accepts integers.

Next came Integration tests. These are high level tests that test the application from the perspective of the user, treating the app as a black box. For example, one integration test would test the sign-in and sign-out features by actually creating an account. Then logging out, watching out for any bugs in the process.

As you can imagine, manually testing each feature of an app is not only a monumental task, but it also introduces the possibility of human-error. The solution to this is writing Automated Tests. These simulate a real user’s interaction with the application, and this is where using Rails really pays off. Rails has a comprehensive suite of tried and tested industry standard tools which cover all aspects of testing.

First up is Guard. This is a handy gem which watches files and runs a desired command after a file is modified. I use this to automatically run tests on a file whenever it gets modified.

Next is Rspec, a framework for automated testing of code. Having been integrated into Rails with perfection, it gels superbly with the rest of the suite. And most importantly, it enables writing code which is human readable.

For Integration Tests, I used the duo of Capybara and Selenium. Capybara is a tool which closely simulates a real-user interacting with a system. This enables one to test in almost 100% real world conditions. Where Capybara falls short is when JavaScript comes into the picture. And Selenium is a specialised tool that handles just that.

 

I also use a nifty gem called Launchy which automatically screenshots failed tests and opens them for viewing. Having a clear overview of where my tests failed greatly sped up my workflow.

That pretty much sums up my test suite. I am currently speeding up testing, as Selenium, out of the box, is quite slow. One promising solution is to make threads execute in parallel.

It’s been a fantastic experience working with a team that’s committed to building a well-thought-out product. I can’t wait to share more of my journey with the rest of the community! Until next time, adios.

Rushil Agrawal

The author Rushil Agrawal