Available for hire
Capybara, RSpec, Swinger and a Dash of Awesome

When trying to get Capybara-rspec working I went googling first, to see how others have done it… People suck and generally over complicate the simplest tasks… Heres how I got it all working!

Gemfile
1
2
3
gem 'rspec-rails'
gem 'capybara'
gem 'swinger'

Add rspec rails, capybara and swinger, then install your gems and generate rspec skeleton files

1
2
bundle install
rails g rspec:install

In your spec/spec_helper.rb file require your gems.. a quick note, I force spec to use “test” env, why wouldn’t it do this by default? I’m sure theres an answer, but it doesn’t feel write, and everything works just fine like this!

spec/spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
ENV["RAILS_ENV"] = 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'swinger'

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.mock_with :rspec
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
end

I really like the idea of my integration tests living in a directory called “spec/integration”, so I create it, and put some tests in there.

/spec/integration/user_integration.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
require "spec_helper"

describe 'A user session' do

  it 'should allow a user to login', :driver => :selenium do
    visit '/'
    click_link 'Login'
    fill_in 'Email', :with => "user@markyg.com"
    fill_in 'Password', :with => "password"
    click_button 'Sign in'
    find('Signed in successfully')
  end
end

A quick note, swinger allows us to add :driver => :selenium, or :driver => :culerity to our specs, this is how we can easily switch drivers.. if you leave this out, it’ll use rack.