Idyllize
Idyllize

Reputation: 47

NameError: uninitialized constant Minitest::VERSION

I'm using Rails 4.1 and Ruby 2.0.0. I'm trying to set up testing with minitest-rails and I'm running into this strange error. When I include:

require 'minitest/spec'

In my 'spec_helper' file it give me a NameError: uninitialized constant Minitest::VERSION error. When I comment out this line, everything seems to work fine. The odd thing is that 'minitest/autorun' is also in there and not causing any problems. Maybe you guys can shed some light on what's going on here.

spec_helper.rb:

ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)

require 'minitest/spec'
require 'minitest/autorun'
require 'minitest-rails'
require 'minitest-rails-capybara'

Rakefile:

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

    Pinteresting::Application.load_tasks

    namespace :test do
      task :run do
        ENV["RACK_ENV"] = "test"
        $LOAD_PATH.unshift("lib", "spec")
        if ARGV[1]
          require_relative ARGV[1]
        else
          Dir.glob("./spec/**/*_spec.rb").each { |file| require file }
        end
      end
    end

.spec:

    require "spec_helper"

    describe "Test" do
      describe "When two is equal to two" do 
        it "asserts true" do
          assert_equal(2, 2)
        end
      end
    end

Stack trace:

    nbp-93-202:pinteresting Frank$ rake test:run
    rake aborted!
    NameError: uninitialized constant Minitest::VERSION
    /usr/local/rvm/gems/ruby-2.0.0-p481/gems/minitest-5.3.4/lib/minitest/unit.rb:22:in `<class:Unit>'
    /usr/local/rvm/gems/ruby-2.0.0-p481/gems/minitest-5.3.4/lib/minitest/unit.rb:21:in `<module:Minitest>'
    /usr/local/rvm/gems/ruby-2.0.0-p481/gems/minitest-5.3.4/lib/minitest/unit.rb:20:in `<top (required)>'
    /usr/local/rvm/gems/ruby-2.0.0-p481/gems/minitest-5.3.4/lib/minitest/spec.rb:1:in `<top (required)>'
    /Users/Frank/Desktop/pinteresting/spec/spec_helper.rb:4:in `<top (required)>'
    /Users/Frank/Desktop/pinteresting/spec/diagnostic_spec.rb:1:in `<top (required)>'
    /Users/Frank/Desktop/pinteresting/Rakefile:12:in `block (3 levels) in <top (required)>'
    /Users/Frank/Desktop/pinteresting/Rakefile:12:in `each'
    /Users/Frank/Desktop/pinteresting/Rakefile:12:in `block (2 levels) in <top (required)>'
    Tasks: TOP => test:run

Upvotes: 2

Views: 5013

Answers (2)

Eduardo
Eduardo

Reputation: 2704

Interestingly, if try to run or require a file with just the two requires minitest/spec and minitest/autorun the interpreter raises a warning saying that you should require 'minitest/autorun' instead or add "gem 'minitest'" before require 'minitest/autorun', although it doesn't rise the NameErrorto me.

So switching the require statements around (in order to first require minitest/autorun) seems to do the trick. Requiring minitestin the first place seems to do the trick also.

Upvotes: 3

blowmage
blowmage

Reputation: 8984

I think you can resolve this warning by making your implementation simpler. In spec/spec_helper.rb:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/rails"
require "minitest/rails/capybara"

You are missing the require for rails/test_help. Did you remove that for a specific reason?

In Rakefile:

# 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__)

Rails.application.load_tasks

Rails::TestTask.new("test:spec" => "test:prepare") do |t|
  t.pattern = "spec/**/*_spec.rb"
end

Rake::Task["test:run"].enhance ["test:spec"]

And now run either $ rake test:spec to run all your specs, or $ rake test to run all your tests. The reason behind keeping the rake tasks under the test namespace is because this is what Spring keys on to use the running test environment. Spring uses the task namespace, not the directory name.

Upvotes: 0

Related Questions