Skip to content

Instantly share code, notes, and snippets.

@gangelo
Created September 20, 2014 18:41
Show Gist options
  • Select an option

  • Save gangelo/af5977a18390a2badd18 to your computer and use it in GitHub Desktop.

Select an option

Save gangelo/af5977a18390a2badd18 to your computer and use it in GitHub Desktop.
database_cleaner, rspec-rails and capybara configuration strategy
#
# What's this do?
#
# Keep your test database clean whilst testing, using database_cleaner, rspec-rails and capybara.
#
# Instructions:
#
# Place this file in spec/support
#
# Assumptions:
#
# ruby - https://github.com/ruby/ruby
# rails - https://github.com/rails/rails/
# rspec-rails - https://github.com/rspec/rspec-rails | https://relishapp.com/rspec/rspec-rails/docs
# capybara - https://github.com/jnicklas/capybara | http://jnicklas.github.io/capybara/
# database_cleaner - https://github.com/DatabaseCleaner/database_cleaner
#
# Tested using the following versions:
#
# ruby '2.1.0'
# rails '4.1.1'
# rspec-rails '~> 3.0.2'
# capybara '~> 2.4.1'
# database_cleaner '~> 1.3.0'
#
# Assumes capybara spec/features specs IAW capybara suggested usage.
#
# Credits:
#
# From http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/
# See also https://coderwall.com/p/ahtb7w
RSpec.configure do |config|
#
# If you're not using ActiveRecord, or you'd prefer not to run
# each of your examples within a transaction, remove the following
# line or assign false instead of true.
config.use_transactional_fixtures = false
#
# Clean up and initialize database before
# running test exmaples.
config.before(:suite) do
#
# Truncate database to clean up garbage from
# interrupted or badly written examples.
DatabaseCleaner.clean_with(:truncation)
#
# https://coderwall.com/p/ahtb7w
#
# Seed dataase. Use it only for essential
# to run application data.
load "#{Rails.root}/db/seeds.rb"
end
#
# Use really fast transaction strategy for all
# examples...
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
#
# ...except `js: true` capybara specs.
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
#
# Start transaction.
DatabaseCleaner.start
end
config.after(:each) do
#
# Rollback transaction.
DatabaseCleaner.clean
end
#
# ttps://coderwall.com/p/ahtb7w
config.after(:each) do
#
# Clear session data.
Capybara.reset_sessions!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment