No way to test lib/* by default #59

dlee opened this Issue Jul 12, 2012 · 8 comments

5 participants


There's no way to unit test code under lib/* since TASKS (in minitest.rake) doesn't include lib, and it doesn't make sense to stuff the tests into any of the other subdirectories.

Would it make sense to add lib to TASKS?


I was actually working on updating the rake tasks this morning. I'll commit the code tonight and it make testing new directories under test much easier. It'll be as easy as adding MINITEST_TASKS << "models" to the end of your Rakefile.

@blowmage blowmage added a commit that closed this issue Jul 13, 2012
@blowmage Update rake tasks (closes #59)
Restore test:prepare task, which is what libs like ActiveRecord hook onto.
Consolidate constants to just MINITEST_TASKS, so we only add tasks in one place.
Rename `minitest:run` to `minitest:default`.
Add tasks for every directory under test, even if it isn't in MINITEST_TASKS.
Add "all" task to run all the tests.
@blowmage blowmage closed this in 3347b1a Jul 13, 2012

You can make minitest-rails aware of your "lib" code by using the master branch. This will be included in the 0.1.1 release. To use master, update your Gemfile like so:

group :test, :development do
  gem "minitest-rails", git: "git://"

Next create your tests files for your lib code in test/lib. You can now run those tests by typing the following:

rake minitest:lib

Or you can add "lib" to the default minitest tasks by adding the following MINITEST_TASKS line to the end of your Rakefile:

#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require File.expand_path('../config/application', __FILE__)



With this updated Rakefile, your "lib" tasks will run when you type:

rake minitest


rake test

Is this still relevant? I'm trying to lib tests in isolation using

rake test TEST=test/lib/line_item_cost_processor_test.rb

I added MiniTest::Rails::Testing.default_tasks << 'lib' to my rake task, and moved the gem OUT of the test group (which surprised me), but i'm now stuck with a uninitialized constant LineItemCostProcessor exception. Any tips here on how to configure this properly?

UPDATE: Interesting — when I run tests ungrouped using rake minitest:all:quick i do not run into these issues. Hrm.


Try adding require "line_item_cost_processor" to your test file. Rails does not autorequire code in lib, and that is why you get an uninitialized constant. When you run all tests you are likely loading code that does have the require, which is why the test passes.


Yeah, tried that which results in:

/Users/xxx/Sources/api/test/lib/line_item_cost_processor_test.rb:3:in `<top (required)>': undefined method `describe' for main:Object (NoMethodError)

So minitest isn't loading the spec-style test helpers?

ruby error is:

Command failed with status (1): [ruby -I"lib:test" -I"/Users/xxx/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.1.0/lib" "/Users/xxx/.rvm/gems/ruby-2.0.0-p0/gems/rake-10.1.0/lib/rake/rake_test_loader.rb" "test/lib/**/*_test.rb" ]

I'd have to see your test and helper code to say for sure. But at this point I suspect this is a configuration issue in your app and not a problem with minitest-rails.

That said, the issue you are having isn't related to this issue, so let's stop troubleshooting here. Hop on freenode's #minitest channel or send an email to the mailing list or to me directly.


Zombie thread, but I came across this.

MINITEST_TASKS and MiniTest::Rails::Testing.default_tasks have both been removed.

Solution is to create a lib/tasks/test.rake file which includes:"test:lib" => "test:prepare") do |t|
  t.pattern = "test/lib/**/*_test.rb"
Rake::Task["test:run"].enhance ["test:lib"]

As of rails 4.2, all tests in the test/ directory are loaded by default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment