Skip to content

Instantly share code, notes, and snippets.

@krishna-shilpakar
Created August 15, 2012 08:36
Show Gist options
  • Save krishna-shilpakar/3357655 to your computer and use it in GitHub Desktop.
Save krishna-shilpakar/3357655 to your computer and use it in GitHub Desktop.
Rails with rspec, capybara

#Rails with Rspec, Capybara (with selenium driver), FactoryGirl #

Generate new app, skipping Test::Unit file generation and skipping bundle

rails new my_app -T --skip-bundle

Setup Gemfile

  # in Gemfile

  group :test do
    gem 'database_cleaner'
    gem 'capybara'
    gem 'factory_girl_rails'
    gem 'rspec-rails'
  end

Install Gems

Bundle Install

Configure generators to use the gems we want, and skip view spec generation

  # in config/application.rb

  config.generators do |g|
    g.test_framework :rspec, :views => false, :fixture => true
    g.fixture_replacement :factory_girl, :dir => 'spec/factories'
  end

Install generators

rails generate rspec:install

Update spec_helper

  # in spec/spec_helper.rb

  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'

  # Add this to load Capybara integration:
  require 'capybara/rspec'
  require 'capybara/rails'

Database Cleaner

  # spec/support/database_cleaner.rb

  DatabaseCleaner.strategy = :truncation
  RSpec.configure do |config|
    config.use_transactional_fixtures = false
    config.before :each do
      DatabaseCleaner.start
    end
    config.after :each do
      DatabaseCleaner.clean
    end
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment