Scott Weisman

Easy Rails Testing

July 19, 2012

Every beginning Rails developer wants to tests their code. Unfortunately, most spend too much time deciding between Test::Unit and RSpec, get confused as to what types of tests to write, and eventually give up.

If you're a beginner and want to test, start by learning how to write integration (Test::Unit) or request (RSpec) tests. These types of tests allow you to simulate what the user will actually do in the browser. Capybara is a gem that makes it easy to write integration/request tests. If you use Capybara, the difference between Test::Unit and RSpec is minimal.

Here's a simple RSpec request spec (test) with Capybara for user sign up:

require 'spec_helper'
describe "Users" do
  it "allows users to sign up" do
    visit root_path
    click_link 'Sign up'
    fill_in 'Email', with: ''
    fill_in 'Password', with: 'secret'
    click_button 'Sign up'
    page.should have_content('Welcome')

You don't need much programming experience to understand what's going on here. The test is just going through the same sign up process that a user visiting your site would. The final line of the test page.should have_content('Welcome') is stating what the next page should show after a user clicks the 'Sign up' button. Simple, right? So go write some tests!

Check out Railscast #275 for more information on this type of testing.