Where developers come to connect, share, build and be inspired.

21

Cut down some seconds of your spec suite by removing database_cleaner

2124 views

The gem database_cleaner is a beast and may eat some seconds out of your rspec test suite. By using a simpler approach you can save up some time.

Here's how:

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

The gist here.

The check for :js environment is necessary if you use capybara-webkit.

Many thanks to @brandonhilkert for pointing it out here.

With this change, my suite got 40% faster!!


endorse

Comments

Add a comment