Search code examples
ruby-on-railsrspec-rails

Overriding helper method in Rails


I have following line in my rails helper.

  config.use_transactional_fixtures = true

But I want to modify it to

  config.use_transactional_fixtures = false

But I don't want to modify it for every tests inside spec folder. I just want to apply it to all the tests inside spec/requests folder.
How can I do it ?


Solution

  • After doing a bit more research, it looks like you need to use the DatabaseCleaner gem https://github.com/DatabaseCleaner/database_cleaner. In your rails helper, you would add this:

    require 'database_cleaner'
    
    RSpec.configure do |config|
      config.use_transactional_fixtures = false
    
      config.before type: :request do
        DatabaseCleaner.strategy = :truncation
      end
    
      config.after type: :request  do
        DatabaseCleaner.strategy = :transaction
      end
    
      config.before :each do
        DatabaseCleaner.start
      end
    
      config.after do
        DatabaseCleaner.clean
      end
    end
    

    This will set you up so that request specs will use the truncation strategy (removing all data from the database) and everything but request specs use transactions (rollback all changes from runnning scenario).