我在 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
设置和拆卸不执行。为什么?我可以解决这个问题吗?
答案 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。