Sha256: a5e336abe4d2eb1a0f2b42c1c007f16320432f01a304d6619ef4c71dfc2f1c00

Contents?: true

Size: 1.83 KB

Versions: 7

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

# https://github.com/DatabaseCleaner/database_cleaner
# https://medium.com/brief-stops/testing-with-rspec-factorygirl-faker-and-database-cleaner-651c71ca0688
# Flush the database during tests to keep test data from clustering or hitting other tests
require 'capybara/rspec'
require 'database_cleaner'

RSpec.configure do |config|
  config.before(:suite) do
    if config.use_transactional_fixtures?
      raise(<<-MSG)
        Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
        (or set it to false) to prevent uncommitted transactions being used in
        JavaScript-dependent specs.

        During testing, the app-under-test that the browser driver connects to
        uses a different database connection to the database connection used by
        the spec. The app's database connection would not be able to access
        uncommitted transaction data setup over the spec's database connection.
      MSG
    end

    DatabaseCleaner.clean_with(:truncation)
  end

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

  config.before(:each, type: :feature) do
    # :rack_test driver's Rack app under test shares database connection
    # with the specs, so continue to use transaction strategy for speed.
    driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test

    unless driver_shares_db_connection_with_specs
      # Driver is probably for an external browser with an app
      # under test that does *not* share a database connection with the
      # specs, so use truncation strategy.
      DatabaseCleaner.strategy = :truncation
    end
  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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
kowl-0.0.7 lib/kowl/templates/tests/rspec/support/database_cleaner.rb
kowl-0.0.6 lib/kowl/templates/tests/rspec/support/database_cleaner.rb
kowl-0.0.5 lib/kowl/templates/tests/rspec/support/database_cleaner.rb
kowl-0.0.4 lib/kowl/templates/tests/rspec/support/database_cleaner.rb
kowl-0.0.3 lib/kowl/templates/tests/rspec/support/database_cleaner.rb
kowl-0.0.2 lib/kowl/templates/tests/rspec/support/database_cleaner.rb
kowl-0.0.1 lib/kowl/templates/tests/rspec/support/database_cleaner.rb