12

I use Ruby 2 and Rails 4. I have a folder test/lib, where a few tests are located. But running rake test does not use them. Only the other tests (models, controllers, ...) are running.

Where do I have to add the lib folder?

I already tried MiniTest::Rails::Testing.default_tasks << 'lib', but I get NameError Exception: uninitialized constant MiniTest::Rails. I did not add the minitest gem to my Gemfile, because Ruby 2 uses it by default.

3
  • you can override the task rake test in Rakefile by creating a new task with executing `ruby -Ilib:test "test/lib/*" Sep 19, 2013 at 12:33
  • 4
    The simplest solution for me is to use rake test:all instead of rake test
    – Bjoernsen
    Sep 20, 2013 at 7:27
  • For Rails 4.2 rake test:all is deprecated and will be removed in Rails 5. But rake test now works exactly the same.
    – Bjoernsen
    Mar 26, 2015 at 12:14

1 Answer 1

13

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 }
3
  • Thanks for this! I think you mean "add the following to your Rakefile", not Gemfile though. Jan 30, 2014 at 23:15
  • 1
    on rails 4 this adds 3 seconds to boot, just for an empty test/lib dir
    – Krut
    Apr 21, 2014 at 17:45
  • Is this supposed to be Rake::TestTask or Rails::TestTask? First one seems to produce many errors before actually running the tests.
    – Tashows
    Dec 18, 2016 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.