Skip to content

Instantly share code, notes, and snippets.

@oojikoo-gist
Created April 17, 2015 20:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oojikoo-gist/fc322f5bb6d1f8443c39 to your computer and use it in GitHub Desktop.
Save oojikoo-gist/fc322f5bb6d1f8443c39 to your computer and use it in GitHub Desktop.
rails: rspec3

Rails 4 with Rspec 3

Configure your Gemfile

# Gemfile
group :development, :test do
  gem 'rspec-rails', '~> 3.0.0'
  gem 'factory_girl_rails'
  gem 'capybara'
  gem 'database_cleaner'
end

Run generators

$ rails generate rspec:install

Congifure Capybara

To make Capybara available from within RSpec specs, add the following line to spec/rails_helper.rb:

# spec/rails_helper.rb
require 'capybara/rails'

Your Capybara feature specs will also need a home. Add the spec/features/ directory:

mkdir spec/features

Or You Can run:

$ rails generate rspec:feature [test_name]

then it will test under spec/features

Configure database_cleaner

Next, make the following adjustments to spec/rails_helper.rb to integrate the database_cleaner gem:

# spec/rails_helper
RSpec.configure do |config|
  config.use_transactional_fixtures = false
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end

Include the factory_girl methods

To make the factory_girl gem’s methods (e.g., build and create) easily available in RSpec examples, add this line to the top of your RSpec.configure block in spec/rails_helper.rb:

# spec/rails_helper.rb
RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods

  # other configurations below...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment