Rails – tools for testing

One of the great things about Ruby and Rails is the sheer number of gems out there that are available to make your life easier. There are many gems out there that are designed to help make your work better and even reduce the effort required to produce beautiful code. I’m going to outline a few of the gems that I’ve been using a lot recently while learning how to make Rails apps, specifically for testing. These gems are extremely useful tools in making sure that your code functions as expected and can increase your productivity.


quiet_assets allows you to turn off the overwhelming amount of messages that are printed in the rails server log. It will help you keep track of the important things going on in your rails app without having to read about how your app is accessing the database.

GitHub repo


rspec-rails is just the rails version of the popular testing gem, rspec. It’s a great way to use TDD to help create your app. It’s use will be very familiar if you’ve ever use rspec to test Ruby code. However, it’s definitely got a whole other suite of methods that are useful for testing your code in Rails. If you want to start getting familiar with it’s use, visit the Relish documentation.

If you’re going to use rspec-rails, don’t forget to add some code into your spec_helper.rb to reduce the amount of error messages that you have to read.

GitHub repo


capybara is another great rails testing tool based on Behavior Driven Development. It is used to create tests that predict how a user might interact with your rails app. In this way, it makes it easier to identify small problems that would detract from a user’s experience.

For example, I’ve written a test below that describes a scenario where a user might want to create a new profile. The great thing about capybara is that it uses syntax that reads very close to a natural description of what a user would do to create a new profile. This makes it easier to write simple, clear tests so that you can focus more on writing code.

require 'rails_helper'

feature 'Creating a new profile' do
  scenario 'should allow a user to make a profile' do
    visit '/'

    click_button 'Sign Up'

    expect(page).to have_content('Thank you for signing up!')

    fill_in 'First Name', with: 'Tim'
    fill_in 'Last Name', with: 'Park'
    fill_in 'Date of Birth', with: '01/01/01'
    fill_in 'Phone Number', with: '1234567890'
    fill_in 'Email', with: 'timsjpark@gmail.com'
    click_button 'Save new profile'

    expect(page).to have_content('Welcome to your new home page!')

GitHub repo


shoulda-matchers allows you to write simple code for complex tests for common Rails functions. You can test if a link does a redirect_to a page or validate_inclusion_of a required field in a form submission.

GitHub repo


factory_girl_rails helps create stock objects with extremely simple code. It takes the time and effort required to define something like a product before writing a test for it. Instead of generating a fake product by calling on your model, why not let factory_girl do all of the hard work.

product = Product.create(type: 'Computer', make: 'Company X', model: X100, color: black, price: 2000)

The factory_girl_rails way

product = FactoryGirl.create(:product)

GitHub repo


faker is an amazing gem that can populate your database with fake data. It can accommodate a number of varied types of data such as names, addresses, commercial products, dates, and even entire paragraphs. When used in tandem with factory_girl_rails, it’s extremely valuable for creating test data that will help your Rails app feel like a real product.

GitHub repo

Remember, Test and Behavior Driven Development are important tools in quickly writing code that works well. But if you’re spending all of your time trying to figure out how to write tests and specify how you want your app to behave, then you end up being counter-productive. These gem will help remove the effort required to write good tests so you can get out there and have fun building Rails apps.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s