NameError:未初始化的常量Minitest :: VERSION

时间:2014-06-16 15:58:34

标签: ruby-on-rails ruby minitest

我正在使用Rails 4.1和Ruby 2.0.0。我正在尝试使用minitest-rails设置测试,我遇到了这个奇怪的错误。当我包括:

require 'minitest/spec'

在我的'spec_helper'文件中,它给我一个NameError:未初始化的常量Minitest :: VERSION错误。当我注释掉这一行时,一切似乎都很好。奇怪的是'minitest / autorun'也在那里,并没有造成任何问题。也许你们可以对这里发生的事情有所了解。

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

堆栈追踪:

    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

2 个答案:

答案 0 :(得分:3)

有趣的是,如果尝试运行或要求只有两个文件的文件需要minitest/specminitest/autorun,解释程序会发出一条警告说you should require 'minitest/autorun' instead or add "gem 'minitest'" before require 'minitest/autorun',尽管它没有上升NameError给我。

因此,切换require语句(首先需要minitest/autorun)似乎可以解决问题。首先要求minitest似乎也可以做到这一点。

答案 1 :(得分:0)

我认为您可以通过简化实施来解决此警告。在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"

您缺少rails/test_help的要求。您是否因特定原因删除了该内容?

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"]

现在运行$ rake test:spec运行所有规范,或$ rake test运行所有测试。将rake任务保留在test命名空间后面的原因是因为这是使用正在运行的测试环境的Spring键。 Spring使用任务命名空间,而不是目录名。