Skip to content

Instantly share code, notes, and snippets.

@jacobvosmaer
Last active December 11, 2015 08:19
Show Gist options
  • Save jacobvosmaer/4572583 to your computer and use it in GitHub Desktop.
Save jacobvosmaer/4572583 to your computer and use it in GitHub Desktop.

Acceptance tests with Capybara and Rspec (quick and dirty)

A simple acceptance test for the RailsGirls Ideas App.

Installation

Add the following lines to your Gemfile:

gem 'rspec-rails'
gem 'capybara'

Install the gems by running

bundle install

Setup RSpec and Capybara

Run the following command to set up the test runner, RSpec:

rails generate rspec:install

Next, we will enable Capybara, our browser test library:

  • Open spec/spec_helper.rb
  • underneath require 'rspec/autorun' add the following line:
require 'capybara/rails'

This is telling RSpec we want to use Capybara.

  • (optional) underneath RSpec.configure do |config|, add:
config.before { Capybara.default_driver = :selenium }

This tells Capybara that we want to use the 'Selenium' driver; this is a tool for automatically clicking through our app in a Firefox window.

Write a test!

Create a new file spec/ideas_spec.rb with the following content:

require 'spec_helper'

feature "Ideas" do

  scenario "start at the ideas page" do
    visit '/'

    page.should have_content 'Ideas'
  end

end

We can now run all our tests (right now there's only one) by running:

bundle exec rspec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment