Skip to content

@felipeelias /spec_helper.rb
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Simple 'database cleaner' approach using `config.around`. Was able to cut down some seconds out of the suite. Thanks to @brandonhilkert
RSpec.configure do |config|
config.around do |example|
# For examples using capybara-webkit for example.
# Remove this if you don't use it or anything similar
if example.metadata[:js]
example.run
ActiveRecord::Base.connection.execute("TRUNCATE #{ActiveRecord::Base.connection.tables.join(',')} RESTART IDENTITY")
else
ActiveRecord::Base.transaction do
example.run
raise ActiveRecord::Rollback
end
end
end
end
@dgilperez

Niiiice stuff !!! 33% improvement on my large and slow test suite :)

I think this code is valid for PostgreSQL, but not for MySQL. For MySQL we are currently substituting this line:

ActiveRecord::Base.connection.execute("TRUNCATE #{ActiveRecord::Base.connection.tables.join(',')} RESTART IDENTITY")

For this ones:

ActiveRecord::Base.connection.tables.each do |table|
  ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table};")
end
@felipeelias
Owner

@dgilperez awesome! And yes, the code is valid only for postgreSQL

@jfelchner

Just a note that the speed bump you're getting here is less from not loading database_cleaner and more from the fact that you're only truncating on :js examples. You can do the same thing with a specific database cleaner configuration. So you can get a database agnostic cleaning approach and a pretty fast suite in the same solution.

@felipeelias
Owner

@jfelchner you're right, I just found myself not in need of using it on projects that has one required database (so I don't need to be agnostic), but for sure it's quite helpful. It was a bit of an overhead to have database_cleaner imo.

@adriancb

I saw a dramatic improvement in the test suite I'm working with... When I mean dramatic, I mean 8 minutes to 27 seconds...

I'm running jruby 1.7.4 (1.9.3p392) 2013-05-16 2390d3b on Java HotSpot(TM) 64-Bit Server VM 1.6.0_51-b11-457-11M4509 [darwin-x86_64]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.