LoadError:无法加载此类文件 - minitest / rails

时间:2017-02-21 13:09:56

标签: ruby-on-rails ruby-on-rails-4 minitest

我正在使用Rails 4.2和Ruby 2.1.3,同时使用以下命令运行单元测试用例

rake test /../test/models/file_test.rb --trace

我收到以下错误

** Invoke test (first_time)
** Execute test
** Invoke test:run (first_time)
** Execute test:run
rake aborted!
LoadError: cannot load such file -- minitest/rails
/opt/library/lib/ruby/gems/2.1.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/opt/library/lib/ruby/gems/2.1.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
/opt/library/lib/ruby/gems/2.1.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/opt/library/lib/ruby/gems/2.1.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/opt/myapp/test/test_helper.rb:4:in `<top (required)>'
/opt/myapp/test/test/controllers/packager_controller_test.rb:1:in `require'
/opt/myapp/test/controllers/packager_controller_test.rb:1:in `<top (required)>'
/opt/library/lib/ruby/gems/2.1.0/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `require'
/opt/library/lib/ruby/gems/2.1.0/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/opt/library/lib/ruby/gems/2.1.0/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/opt/library/lib/ruby/gems/2.1.0/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/opt/library/lib/ruby/gems/2.1.0/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/opt/library/lib/ruby/gems/2.1.0/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.rb:248:in `call'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.rb:248:in `block in execute'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.rb:243:in `each'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.rb:243:in `execute'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/opt/library/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.rb:180:in `invoke_with_call_chain'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.rb:173:in `invoke'
/opt/library/lib/ruby/gems/2.1.0/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/opt/library/lib/ruby/gems/2.1.0/gems/railties-4.2.1/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.rb:248:in `call'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.rb:248:in `block in execute'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.rb:243:in `each'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.rb:243:in `execute'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/opt/library/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.rb:180:in `invoke_with_call_chain'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/task.rb:173:in `invoke'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/application.rb:152:in `invoke_task'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/application.rb:108:in `block (2 levels) in top_level'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/application.rb:108:in `each'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/application.rb:108:in `block in top_level'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/application.rb:117:in `run_with_threads'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/application.rb:102:in `top_level'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/application.rb:80:in `block in run'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/application.rb:178:in `standard_exception_handling'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/lib/rake/application.rb:77:in `run'
/opt/library/lib/ruby/gems/2.1.0/gems/rake-11.3.0/exe/rake:27:in `<top (required)>'
/opt/library/bin/rake:23:in `load'
/opt/library/bin/rake:23:in `<main>'
Tasks: TOP => test:run

的Gemfile

gem 'minitest', '~> 5.1'

根据SO中提供的建议尝试了以下命令,但仍然出现错误。

require File.expand_path('../../test_helper', __FILE__) in file_test.rb

也试过这种方式来运行测试:

ruby -Itest test/models/file_test.rb

test_helper.rb中

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

# To add Capybara feature tests add `gem "minitest-rails-capybara"`
# to the test group in the Gemfile and uncomment the following:
# require "minitest/rails/capybara"

# Uncomment for awesome colorful output
require "minitest/pride"

class ActiveSupport::TestCase
  # ActiveRecord::Migration.check_pending!

  # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
  #
  # Note: You'll currently still have to declare fixtures explicitly in integration tests
  # -- they do not yet inherit this setting
  # fixtures :all

  # Add more helper methods to be used by all tests here...
end

提前致谢。如果我需要提供更多解释,请告诉我。

2 个答案:

答案 0 :(得分:0)

我怀疑问题在这里:

/opt/myapp/test/test_helper.rb:4

那条线是什么? minitest / rails是否包含任何机会?

另请注意,minitest-rails不是最小的。如果你已经添加了minitest-rails,我会尝试删除它(以及包含行)并查看它的位置。

检查4.2 Rails应用程序的全新安装,我在test_helper中看不到这样的包含行 - 你是否添加了它?此外,minitest已经是Rails的依赖项,所以我认为你不需要将它添加到你的Gem文件中。

答案 1 :(得分:0)

我使用Minitest的方式是foreach (ReportFile drv in dataGrid1.SelectedItems.OfType<ReportFile>()) { //... } ,注意它是Gemfile,而不只是minitest-rails。如果你不想这样,你当然可以跳过minitest,但它对于功能/集成测试很有用。

capybara

然后在group :development, :test do gem 'minitest-rails' gem 'minitest-rails-capybara' end

test_helper.rb