Skip to content

Instantly share code, notes, and snippets.

@mockdeep
Last active October 7, 2019 07:19

Revisions

  1. mockdeep revised this gist Mar 31, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions spec_helper.rb
    Original file line number Diff line number Diff line change
    @@ -14,25 +14,30 @@
    DatabaseCleaner.clean_with(:truncation)
    end

    # second on feature specs, first otherwise
    config.before(:each) do
    DatabaseCleaner.start
    end

    # third on feature specs, second/last otherwise
    config.after(:each) do
    p cleaner_strategy
    DatabaseCleaner.clean
    end

    # first on feature specs
    config.prepend_before(:each, :type => :feature) do
    DatabaseCleaner.strategy = :truncation
    end

    # last/fourth on feature specs
    config.append_after(:each, :type => :feature) do
    DatabaseCleaner.strategy = :transaction
    end

    end

    # how to get at what strategy is active, if you like
    def cleaner_strategy
    active_record_cleaner.instance_variable_get(:@strategy).class
    end
  2. mockdeep created this gist Mar 31, 2014.
    42 changes: 42 additions & 0 deletions spec_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    ENV["RAILS_ENV"] ||= 'test'
    require File.expand_path("../../config/environment", __FILE__)
    require 'rspec/rails'
    require 'capybara/rspec'

    RSpec.configure do |config|

    config.use_transactional_fixtures = false
    config.treat_symbols_as_metadata_keys_with_true_values = true

    # before everything
    config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
    end

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

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

    config.prepend_before(:each, :type => :feature) do
    DatabaseCleaner.strategy = :truncation
    end

    config.append_after(:each, :type => :feature) do
    DatabaseCleaner.strategy = :transaction
    end

    end

    def cleaner_strategy
    active_record_cleaner.instance_variable_get(:@strategy).class
    end

    def active_record_cleaner
    DatabaseCleaner.instance_variable_get(:@cleaners)[[:active_record, {}]]
    end