如果我单独运行,我的 Minitest 控制器测试工作正常rake minitest:controllers
但当我跑步时rake minitest:all
然后我收到验证失败错误。这是因为模型测试中已经使用了电子邮件。我使用DatabaseCleaner清理数据库但无法清理数据库。
我的数据库清理代码:
require "database_cleaner"
DatabaseCleaner.strategy = :transaction
class MiniTest::Rails::ActionController::TestCase
include Devise::TestHelpers
def setup
DatabaseCleaner.start
end
def teardown
DatabaseCleaner.clean
end
简短回答:
gem install "minitest-around"
长答案:
minitest 中的之前/之后或设置/拆卸是NOT与 rspec 中一样的钩子,因此在 minitest 中不能有多个之前/之后或设置/拆卸,因为它们所做的只是重新定义方法。
要解决这个问题,您可以使用minitest-around
,这增加了对多个before
/after
or setup
/teardown
and around
,只需将 gem 添加到您的测试组中:
# put in your Gemfile
gem 'minitest-around', group: :test
对于设置database_cleaner,您可以根据需要进行设置,以下是设置示例:
# tests/support/database_cleaner.rb
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
class Minitest::Rails::ActionController::TestCase
def setup
DatabaseCleaner.start
end
def teardown
DatabaseCleaner.clean
end
end
在你的测试文件中:
# tests/your/test/file_test.rb
require 'support/database_cleaner'
# assertions here ...
就是这样,看Github获取详细信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)