Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Minitest, require 'minitest_helper' load error

I'm testing out Minitest::Spec as an alternative to RSpec, but I've got a pesky problem I can't quite spot the answer to:

I've setup some basic specs in spec/models/*_spec.rb. My rails app includes minitest-rails, and I've set my rakefile as follows:

Rake::TestTask.new do |t|
  t.libs.push "lib"
  t.test_files = FileList['spec/**/*_spec.rb']
  t.verbose = true
end
task :default => :test

Now, if I write my spec files like this:

require 'minitest_helper'

describe User do
  ...
end

... and run rake test, I get:

user_spec.rb:1:in `require': cannot load such file -- minitest_helper (LoadError)

however, if I change the require line to

require_relative '../minitest_helper'

Then it works. So, this is functional, but it seems that every example of people using minitest specs I find online has them just calling require 'minitest_helper', not require_relative. So, what am I missing that lets that work for others but not in my situation?

One last piece of info, my helper file looks like this:

# spec/minitest_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)

require "minitest/autorun"
require "minitest/rails"

# Uncomment if you want Capybara in accceptance/integration tests
# require "minitest/rails/capybara"

# Uncomment if you want awesome colorful output
# require "minitest/pride"

class MiniTest::Rails::ActiveSupport::TestCase
  # Add more helper methods to be used by all tests here...
end

Nothing fancy. Thanks for the help!

like image 596
Andrew Avatar asked Sep 23 '12 17:09

Andrew


2 Answers

Your tests aren't finding the helper file because you haven't told your tests to look where it is. try changing your rake task to this:

Rake::TestTask.new do |t|
  t.libs << "lib"
  t.libs << "spec"
  t.test_files = FileList['spec/**/*_spec.rb']
  t.verbose = true
end
task :default => :test
like image 61
blowmage Avatar answered Nov 11 '22 08:11

blowmage


In Ruby 1.9, the working directory is not included in the Ruby load path. You can add it if you want:

$: << "."

...or you can add any other directories which you want to require Ruby files from.

If you see other people writing just:

require 'minitest_helper'

...then doubtless they have done something to their load path (or Rails/Rake has done it for them). You can try p $: inside your Rakefile to see what Rails/Rake do with the load path (if anything).

like image 43
Alex D Avatar answered Nov 11 '22 09:11

Alex D