问题 Rails和MiniTest:添加其他文件夹


我使用Ruby 2和Rails 4.我有一个文件夹 test/lib,其中有一些测试。 但跑步 rake test 不使用它们。只有其他测试(型号,控制器......)正在运行。

我在哪里添加 lib 夹?

我已经试过了 MiniTest::Rails::Testing.default_tasks << 'lib',但我明白了 NameError Exception: uninitialized constant MiniTest::Rails。我没有将minitest gem添加到我的Gemfile中,因为Ruby 2默认使用它。


12942
2017-09-19 12:00


起源

你可以覆盖任务 rake test 在 Rakefile 通过执行`ruby -Ilib:test“test / lib / *创建一个新任务” - Rajarshi Das
对我来说最简单的解决方案就是使用 rake test:all 代替 rake test - Bjoernsen
对于Rails 4.2 rake test:all 已弃用,将在Rails 5中删除。但是 rake test 现在的工作原理完全相同。 - Bjoernsen


答案:


使用 MiniTest::Rails::Testing.default_tasks << 'lib' 你需要添加 MINITEST护栏 宝石到你的Gemfile。它与Minitest分开,并且添加了默认情况下未在Rails中启用的许多Minitest功能。 minitest-rails添加了其他功能,例如为所有具有测试的目录创建rake任务。因此,如果不对Rakefile进行任何更改,您可以运行以下内容:

$ rake minitest:lib

或者,要以旧式方式执行此操作,您可以将以下内容添加到Rakefile:

namespace :test do

  desc "Test lib source"
  Rake::TestTask.new(:lib) do |t|    
    t.libs << "test"
    t.pattern = 'test/lib/**/*_test.rb'
    t.verbose = true    
  end

end

Rake::Task[:test].enhance { Rake::Task["test:lib"].invoke }

这假设您希望在不使用任何数据库夹具的情况下运行lib测试。如果你想要fixtures和数据库事务,那么你应该创建一个依赖于“test:prepare”的rake任务。

namespace :test do

  desc "Test lib source"
  Rake::TestTask.new(:lib => "test:prepare") do |t|    
    t.libs << "test"
    t.pattern = 'test/lib/**/*_test.rb'
    t.verbose = true    
  end

end

Rake::Task[:test].enhance { Rake::Task["test:lib"].invoke }

13
2017-10-01 22:40



谢谢你!我认为你的意思是“将以下内容添加到你的Rakefile”,而不是Gemfile。 - notruthless
是的。谢谢你的纠正。 - blowmage
在rails 4上,这增加了3秒的启动时间,仅用于空的test / lib目录 - Krut
这应该是Rake :: TestTask还是Rails :: TestTask?在实际运行测试之前,第一个似乎产生了许多错误。 - Tashows