Ruby on rails 小型测试和安装/拆卸挂钩
我在test\u helper中有以下代码 如果我写了这样一个测试Ruby on rails 小型测试和安装/拆卸挂钩,ruby-on-rails,ruby,minitest,Ruby On Rails,Ruby,Minitest,我在test\u helper中有以下代码 如果我写了这样一个测试 class MyTest < ActiveSupport::TestCase test 'test' do #some code end end classmytest
class MyTest < ActiveSupport::TestCase
test 'test' do
#some code
end
end
classmytest
执行安装和拆卸
但如果我写这样的测试
class MyTest < ActiveSupport::TestCase
describe 'some test'
before do
@user = FactoryBot.create(:user)
end
it 'first test' do
# some code
end
it 'second test' do
# some code
end
end
end
classmytest
不执行安装和拆卸。为什么?我可以修复它吗?尝试将以下内容添加到您的
测试助手.rb
:
class Minitest::Spec
before :each do
DatabaseCleaner.start
end
after :each do
DatabaseCleaner.clean
end
end
或者,如果您正在使用minitest-around
gem:
class Minitest::Spec
around do |tests|
DatabaseCleaner.cleaning(&tests)
end
end
这里重要的是使用Minitest::Spec
类而不是ActiveSupport::TestCase
有关更多信息,请参阅。只是一种预感,但我想知道minitest/spec是否与安装/拆卸挂钩不兼容。。。如果在
ActiveSupport::TestCase
定义中用before/after块替换这些块,那么它是否有效?@maxpleaner它不起作用,我会收到以下错误':undefined method'before'for ActiveSupport::TestCase:Class(NoMethodError)
class Minitest::Spec
around do |tests|
DatabaseCleaner.cleaning(&tests)
end
end