Testing Rails on Ruby with rspec -- it takes too long because of TRUNCATE tables on loading rspec

857 Views Asked by At

I'm newbie in Ruby and Rails. I just started to work with both.

The application I'm working on uses rspec to run the tests in Ruby on Rails and at each round all the database tables are truncated. Tailing log/test.log displays many lines with TRUNCATE TABLE <table>. I see many posts that uses Database cleaner gem with configurations like:

config.before(:each) do
    DatabaseCleaner.strategy = :transaction
end

that would skip TRUNCATE tables. Or more comprehensive configurations like:

config.before(:suite) do
  DatabaseCleaner.clean_with :transaction
end

config.before(:each) do
  DatabaseCleaner.strategy = :transaction
end

config.before(:all) do
  DatabaseCleaner.start
end

config.after(:all) do

  DatabaseCleaner.clean
end

but it doesn't work.

There are other posts that suggests to set fsync=off in Postgres configuration, other tutorial suggests to set full_page_writes=off too. But I didn't fiund anything about MySQL, the DBMS I'm using.

Is there a similar configuration for MySQL?

1

There are 1 best solutions below

1
Jignesh Gohel On

Following is configuration from my spec/rails_helper.rb

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.

  # References:
  #   https://relishapp.com/rspec/rspec-rails/docs/transactions
  #   http://stackoverflow.com/questions/21493970/databasecleaner-rspec-what-is-the-correct-configuration
  #   http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/
  config.use_transactional_fixtures = false

  # https://github.com/DatabaseCleaner/database_cleaner#rspec-example
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:suite) do
    begin
      DatabaseCleaner.start
    ensure
      DatabaseCleaner.clean
    end
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end


  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!
end

I am using the above since a long time in my years old project. May be you can try this out.

You can also see the references I have noted while I was exploring about using database_cleaner correctly. BTW in my application I am using database_cleaner having version 1.5.3.

Hope that helps. Thanks.