Skip to content

Instantly share code, notes, and snippets.

@caironoleto
Last active November 22, 2017 17:53
Show Gist options
  • Save caironoleto/e67ded3ff6d6a6d2543a62b486142a21 to your computer and use it in GitHub Desktop.
Save caironoleto/e67ded3ff6d6a6d2543a62b486142a21 to your computer and use it in GitHub Desktop.
A faster way to deal with apartment in specs.
# This rake will setup the tenant like rails
# setup the test database.
Rake::Task['db:test:prepare'].enhance do
# Connect in the test database.
Rails.env = 'test'
ActiveRecord::Base.establish_connection('test')
Apartment::Tenant.drop('app') rescue nil
Apartment::Tenant.create('appp')
end
RSpec.configure do |config|
config.before(:suite) do
# Clean all tables to start
DatabaseCleaner.clean_with :truncation
# Use transactions for tests
DatabaseCleaner.strategy = :transaction
# Truncating doesn't drop schemas, ensure we're clean here, app *may not* exist
Apartment::Tenant.drop('app') rescue nil
# Create the default tenant for our tests
end
config.before do
# Start transaction for this test
DatabaseCleaner.start
# Switch into the default tenant
Apartment::Tenant.switch! 'app'
end
config.after do
# Rollback tenant transaction
Apartment::Tenant.switch('app') do
DatabaseCleaner.clean
end
# Reset tentant back to `public`
Apartment::Tenant.reset
# Rollback transaction
DatabaseCleaner.clean
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment