Skip to content

Instantly share code, notes, and snippets.

@aaronjensen
Created August 15, 2013 06:27
Show Gist options
  • Select an option

  • Save aaronjensen/6238709 to your computer and use it in GitHub Desktop.

Select an option

Save aaronjensen/6238709 to your computer and use it in GitHub Desktop.
check for leaking transactions in rspec tests
def check_for_leftovers
tables = ActiveRecord::Base.connection.select_values("show tables")
leftovers = {}
tables.each do |table|
next if %w(schema_migrations vanity_experiments).include? table
count = ActiveRecord::Base.connection.select_value("select count(*) from #{table}")
leftovers[table] = count if count > 0
end
if leftovers.any?
DatabaseCleaner.clean_with :truncation
raise "LEFTOVERS in\n#{leftovers.map {|k,v| "#{k}: #{v}"}.join("\n")}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment