Skip to content

Instantly share code, notes, and snippets.

@namxam
Last active August 29, 2015 14:04
Show Gist options
  • Save namxam/4ed441517782d774af26 to your computer and use it in GitHub Desktop.
Save namxam/4ed441517782d774af26 to your computer and use it in GitHub Desktop.
# spec/support/database_cleaner.rb
#
# Set sane default for database cleaner and add a meta tag to enable database commits.
# This is important if you need to test after_commit callbacks in rails. (i.e. elasticsearch)
#
require 'database_cleaner'
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.around(:each) do |example|
DatabaseCleaner.strategy = example.metadata[:commit] ? :truncation : :transaction
DatabaseCleaner.cleaning do
example.run
end
end
end
RSpec.configure do |config|
INDEXED_MODELS = [BankAccount, Mandate, Transaction]
config.around(:each, search: true) do |example|
WebMock.disable_net_connect!(allow: ENV['ELASTICSEARCH_URL'] || 'localhost:9200')
INDEXED_MODELS.each { |klass| klass.__elasticsearch__.create_index! }
example.run
INDEXED_MODELS.each { |klass| klass.__elasticsearch__.delete_index! }
WebMock.disable_net_connect!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment