Skip to content

Instantly share code, notes, and snippets.

@Whitespace
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Whitespace/d5b38e8082431aedad2b to your computer and use it in GitHub Desktop.
Save Whitespace/d5b38e8082431aedad2b to your computer and use it in GitHub Desktop.
Setup Rails app to use Capybara

Background

Not every app is setup to run capybara tests, but many have an existing testing framework that we'll have to integrate with. Here are some common frameworks we'll come across.

Rspec

If there's no existing testing framework, let's setup this app with rspec, which is preferred.

  • Add the following to Gemfile:

    group :test do
      gem 'rspec-rails'
      gem 'capybara-rails'
      gem 'factory_girl_rails'
    end
  • Run bundle to install. (If you get an error about Gemfile specifying a different ruby version than the local one, you may have to install a different ruby version using rbenv install 1.2.3-p456)

  • Run rails generate rspec:install

  • Change the RSpec.configure do |config| line in spec/spec_helper.rb file to look like this:

    require 'capybara/rspec'
    require 'rails_helper'
    require 'database_cleaner'
    
    RSpec.configure do |config|
      config.include FactoryGirl::Syntax::Methods
    
      config.before(:suite) do
        begin
          DatabaseCleaner.strategy = :transaction
          DatabaseCleaner.clean_with(:truncation)
          FactoryGirl.lint
        ensure
          DatabaseCleaner.clean
        end
      end
    
      config.around(:each) do |example|
        DatabaseCleaner.cleaning do
          example.run
        end
      end
  • For each model in app/models/model_name.rb, run rails generate factory_girl:model ModelName and then change the files in test/factories/model_name.rb

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