require 'database_cleaner' RSpec.configure do |config| config.before(:suite) do DatabaseCleaner.strategy = :truncation DatabaseCleaner.clean_with(:truncation) end config.around(:each) do |example| DatabaseCleaner.cleaning do example.run end end end Setup
%w(states countries) } DatabaseCleaner.clean_with(:truncation) end config.around(:each) do |example| DatabaseCleaner.cleaning do example.run end end end Setup with exceptions
:truncation DatabaseCleaner[:active_record].clean_with(:transaction) DatabaseCleaner[:mongo_mapper].clean_with(:truncation) end config.around(:each) do |example| DatabaseCleaner.cleaning do example.run end end end Setup with multiple ORMs
initialize(opts={}) if !opts.empty? && !(opts.keys - OPTS).empty? msg = <<-EOS The only valid options are #{OPTS.join(', ')}. You specified #{opts.keys. join(',')}. EOS raise ArgumentError, msg end if opts.has_key?(:only) && opts.has_key?(:except) raise ArgumentError, "You may only specify either :only or :except." end @only = opts[:only] @tables_to_exclude = Array((opts[:except] || []).dup).flatten @tables_to_exclude += migration_storage_names @pre_count = opts[:pre_count] @reset_ids = opts[:reset_ids] @cache_tables = opts.has_key?(:cache_tables) ? !!opts[:cache_tables] : true end DatabaseCleaner::Generic::Truncation
def truncate_table(table_name) raise NotImplementedError end def truncate_tables(tables) tables.each do |table_name| self.truncate_table(table_name) end end end DatabaseCleaner::ActiveRecord::Truncation
• Reduce complexity of configuration.rb & base.rb • Release v1.99 with deprecation warnings • Make it easier for people to write their own strategies • Look for more maintainers
require ‘database_cleaner/active_record’ RSpec.configure do |config| config.before(:suite) do DatabaseCleaner.strategy = :truncation DatabaseCleaner.clean_with(:truncation) end config.around(:each) do |example| DatabaseCleaner.cleaning do example.run end end end Setup for ActiveRecord (v2.0)
will get this message database_cleaner 2 will no longer automatically detect your ORM. You must explicitly require the right path: # spec_helper.rb require ‘database_cleaner/active_record’ # Gemfile group :test do gem ‘database_cleaner-active_record’ end If you wish to upgrade to database_cleaner 2.0+, you must change this in your project. Deprecation Warnings (v1.99)