Installing RSpec, FactoryGirl and Capybara with Rails 3.2

Generating a new Rails app without Test::Unit

Let’s get started. Begin by starting a new Rails project in the directory of your choice.

1
rails new my_app -T

This will generate a new app without the Test::Unit directories as usually would accompany a new Rails project

The Gemfile

The first step to installing Rspec with Rails is to have a look at the documentation, which states you install it in both the test and development group.

In your Gemfile, put the following to install rspec-rails, Capybara, and FactoryGirl.

1
2
3
4
5
6
7
8
9
10
11
12
13
source 'https://rubygems.org'

gem 'rails', '~> 3.2.11'

#...

group :test, :development do
  gem "rspec-rails", "~> 2.0"
  gem 'capybara', "~> 2.0.2"
  gem "factory_girl_rails", "~> 4.0"
end

#...

To install these gems, run:

1
bundle

Install RSpec

To install rspec-rails and tell Rails to use RSpec as the default testing framework instead of Test::Unit, run:

1
rails generate rspec:install

Configure the generators

In config/application.rb, add the following code between the ellipses (surrounding code provided for context)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module MyApp
  class Application < Rails::Application

#...

    config.generators do |g|
      g.test_framework :rspec,
        fixtures: true,
        view_specs: false,
        helper_specs: false,
        routing_specs: false,
        controller_specs: true,
        request_specs: true
      g.fixture_replacement :factory_girl, dir: "spec/factories"
    end

#...

  end
end

This will ensure that when you run the Rails generators, (such as rails g controller home) the appropriate spec files will be generated as well.

Using Capybara

To use Capybara in your feature specs, all you need to do is require it from spec/spec_helper.rb (see line 5):

1
2
3
4
5
6
7
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'

#...

As of Capybara 2.0, there is no configuration necessary to get access to the Capybara DSL (visit/page), given they are placed in the right location. See this page for (slightly) more information. When you generate new feature tests, place them in spec/features to use Capybara DSL. Simple enough.

FactoryGirl Definitions

In addition to requiring Capybara, you will want to add an additional line to spec/spec_helper.rb:

1
FactoryGirl.find_definitions

This ensures that the factories in spec/factories will be detected correctly and you can begin to use them without any further configuration.