1

[Aside: I am slowly answering my own question in "a concise recipe for installing, configuring and running minitest under autotest or Guard"]

My environment: Ruby 2.0. Padrino 0.10.7. Minitest 2.6.2. RackTest 0.6.2.

Short form: What is the best way to extend $LOAD_PATH to include my test directory so I can simply require 'test_helper' in my test files?

Long form:

Here's a sample test file. Note the require_relative "../../../test_helper" -- this requires keeping track of the test file relative to the test_helper.

# file: test/models/api/v0/index_test.rb
require_relative '../../../test_helper'

describe 'nobody home' do
  it 'fetch fails' do
    get "/api/v0/a_uri_that_does_not_exist"
    last_response.status.must_equal 404
  end
end

Here's the test helper:

# file: test/test_helper.rb
PADRINO_ENV = 'test' unless defined?(PADRINO_ENV)
require File.expand_path('../../config/boot', __FILE__)

class MiniTest::Unit::TestCase
  include Rack::Test::Methods

  def app
    Demo.tap { |app|  }
  end
end

And finally, the rakefile that drives it (generated by padrino, invoked via padrino rake test):

# file: test/test.rake
require 'rake/testtask'

test_tasks = Dir['test/*/'].map { |d| File.basename(d) }
$stderr.puts("=== test_tasks = #{test_tasks}")

test_tasks.each do |folder|
  Rake::TestTask.new("test:#{folder}") do |test|
    test.pattern = "test/#{folder}/**/*_test.rb"
    test.verbose = true
  end
end

desc "Run application test suite"
task 'test' => test_tasks.map { |f| "test:#{f}" }

So: what would it take to replace the brittle require_relative '../../../test_helper' with a dependable and easily remembered require 'test_helper'?

Community
  • 1
  • 1
fearless_fool
  • 33,645
  • 23
  • 135
  • 217

1 Answers1

6

you need to add libs:

Rake::TestTask.new("test:#{folder}") do |test|
  test.pattern = "test/#{folder}/**/*_test.rb"
  test.verbose = true
  test.libs << 'test' # <-- this
end

Or if you invoke directly it with ruby:

$ ruby -Itest test/test_file.rb
DAddYE
  • 1,719
  • 11
  • 16
  • Verily thou rocketh! That did the trick. (For extra points, head over to the question referenced in the first line of this question and help me figure out how to get it to work with autotest. `rake padrino test` works, autotest runs but doesn't find any files.) – fearless_fool Mar 23 '13 at 20:46