2014-06-16 429 views
2

我正在使用Rails 4.1和Ruby 2.0.0。我正在嘗試使用minitest-rails進行測試,我遇到了這個奇怪的錯誤。當我包括:NameError:未初始化的常量Minitest :: VERSION

require 'minitest/spec' 

在我的「spec_helper」文件,它給我一個NameError:未初始化不斷MINITEST ::版本錯誤。當我評論這條線時,一切似乎都很好。奇怪的是,'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 
+0

運行'bundle install'時你看到這個錯誤嗎?包括堆棧跟蹤將有助於 –

+0

我添加了堆棧跟蹤 – Idyllize

+0

如果更改需求中的順序,即首先需要'minitest/autorun'然後'minitest/spec',那麼錯誤是否仍然會出現? – eduardo

回答

3

有趣的是,如果試圖運行或只需要兩個文件需要一個文件需要minitest/specminitest/autorun解釋器引發一個警告說you should require 'minitest/autorun' instead or add "gem 'minitest'" before require 'minitest/autorun',雖然它並沒有上升到NameError給我。

因此,切換require語句(首先需要minitest/autorun)似乎有訣竅。首先需要minitest似乎也能做到這一點。

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使用任務命名空間,而不是目錄名稱。

相關問題