Minitest规范使用相同的db事务进行一组测试

时间:2014-10-21 18:32:49

标签: ruby-on-rails ruby minitest database-cleaner

我怎样才能在minitest之前使用相同的rspec(:all)。我有一组测试需要永远运行,但如果我不必在每次测试之前设置数据库,它会非常快。

我想做的是:

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

1 个答案:

答案 0 :(得分:2)

Minitest就是Ruby。

我可能只使用BEFORE { }块。在运行测试之前,先获取设置所需的设置。

END { }将成为该过程的另一半。

从Ruby 1.9 Keyword Documentation开始:

# 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
   }

# END
# Designates, via code block, code to be executed just prior to program termination.

   END { puts "Bye!" }