1

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?

Caco
  • 1,601
  • 1
  • 26
  • 53
  • Have you heard about [viewcomponent](https://viewcomponent.org/)s? They are easy and quick to test. – siery Mar 04 '21 at 23:25
  • I have just recently learned about them, as the nonstandard ruby in rails controllers was bugging me. Maybe this will help you to get started: [copy-a-controller-class-instance-variables](https://stackoverflow.com/questions/66433139/copy-a-controller-class-instance-variables). – siery Mar 04 '21 at 23:30
  • 1
    The comments about viewcomponents seem unrelated to what has been asked. – Jignesh Gohel Mar 05 '21 at 13:44

1 Answers1

0

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.

Jignesh Gohel
  • 6,236
  • 6
  • 53
  • 89
  • Thanks for the answer. Unfortunately it didn't work for me. There are lots of tables and each time I run rspec, the load process truncate all the tables and, for each table, it takes an average of about 7 seconds. – Caco Mar 05 '21 at 15:41