Search code examples
ruby-on-railsrubyminitestdatabase-cleaner

Minitest spec using same db transaction for a group of tests


How would I do the equivalent of rspec before(:all) in minitest. I have a group of tests that take forever to run but it would be really fast if I didn't have to setup the database before each test.

What I would like to do is:

before(:all) do
  config.transactional_fixtures = false
  DatabaseCleaner.start
end

......
# block of 6 tests in their own describe block.
......

after(:all) do
  config.transactional_fixtures = true
  DatabaseCleaner.clean
end

Solution

  • Minitest is simply Ruby.

    I would probably just use a BEFORE { } block. Get those things setup that you need to get set up before you run your tests.

    END { } is going to be the other half of that process.

    As of Ruby 1.9 Keyword Documentation states:

    # BEGIN
    # Designates, via code block, code to be executed unconditionally before sequential execution of the program begins. Sometimes used to simulate forward references to methods.
    
       puts times_3(gets.to_i)
    
       BEGIN {
         def times_3(n)
           n * 3
         end
       }
    

    and

    # END
    # Designates, via code block, code to be executed just prior to program termination.
    
       END { puts "Bye!" }