Minitest和安装/拆卸挂钩

时间:2019-01-30 05:48:46

标签: ruby-on-rails ruby minitest

我在 test_helper

中有以下代码
let myString = "\"https://api.example.com/deep-link?url=some_url_encoded_string\"" 

let newString = myString.replacingOccurrences(of: "\", with: "").replacingOccurrences(of: """, with: "")

print(newString)

如果我写这样的测试

require "minitest/spec"
require "minitest/autorun"
require "database_cleaner"

class ActiveSupport::TestCase
  DatabaseCleaner.strategy = :deletion

  include Minitest::Spec::DSL

  setup { DatabaseCleaner.start }
  teardown { DatabaseCleaner.clean }
end

执行设置和拆卸

但是如果我这样编写测试

class MyTest < ActiveSupport::TestCase
  test 'test' do
    #some code
  end
end

设置和拆卸不执行。为什么?我可以解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

尝试将以下内容添加到您的test_helper.rb

class Minitest::Spec
  before :each do
    DatabaseCleaner.start
  end

  after :each do
    DatabaseCleaner.clean
  end
end

或者,如果您使用的是minitest-around宝石:

class Minitest::Spec
  around do |tests|
    DatabaseCleaner.cleaning(&tests)
  end
end

这里重要的是use Minitest::Spec类,而不是ActiveSupport::TestCase

有关更多信息,请参见database cleaner docs