Rails and MiniTest: add additional folder

瘦欲@ 提交于 2019-12-04 00:27:28

To use MiniTest::Rails::Testing.default_tasks << 'lib' you need to add the minitest-rails gem to your Gemfile. It is separate from Minitest and adds enables many Minitest features missing that are not enabled in Rails by default. And minitest-rails adds other features, such as creating rake tasks for all the directories that have tests. So without any changes to your Rakefile you can run things like this:

$ rake minitest:lib

Alternatively, to do this the old fashioned way, you can add the following to your 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 }

This assumes you want to run your lib tests without using any database fixtures. If you want the fixtures and database transactions, then you should create the rake task with a dependency on "test:prepare".

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 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!