【问题标题】:LoadError: cannot load such file -- rspec/core/rake_taskLoadError: 无法加载此类文件 -- rspec/core/rake_task
【发布时间】:2025-12-23 14:10:06
【问题描述】:

当我 rake 时,我得到了这个错误。

AllenLins-MacBook-Pro:geoblacklight allenlin$ rake -t
Resolving dependencies...
You must `gem install bundler` and `bundle install` to run rake tasks
rake aborted!
LoadError: cannot load such file -- rspec/core/rake_task
/Users/allenlin/Documents/USpatial/geoblacklight/Rakefile:12:in `require'
/Users/allenlin/Documents/USpatial/geoblacklight/Rakefile:12:in `<top (required)>'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/rake_module.rb:28:in `load'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/rake_module.rb:28:in `load_rakefile'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/application.rb:689:in `raw_load_rakefile'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/application.rb:94:in `block in load_rakefile'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/application.rb:93:in `load_rakefile'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/application.rb:77:in `block in run'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/Users/allenlin/.rbenv/versions/2.2.2/bin/rake:23:in `load'
/Users/allenlin/.rbenv/versions/2.2.2/bin/rake:23:in `<main>'

但是,我已经运行了 'gem install bundler' 和 'bundle install' rspec/core/rake_task 实际存在,如

所示
AllenLins-MacBook-Pro:geoblacklight allenlin$ gem which rspec/core/rake_task
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.0.4/lib/rspec/core/rake_task.rb

我在这里迷路了。非常感谢您对此的任何帮助!我附上了 Gemfile:

source 'https://rubygems.org'

# Specify your gem's dependencies in geoblacklight.gemspec
gemspec

# If we don't specify 2.11.0 we'll end up with sprockets 2.12.0 in the main
# Gemfile.lock but since sass-rails gets generated (rails new) into the test app
# it'll want sprockets 2.11.0 and we'll have a conflict
gem 'sprockets', '2.11.0'

# If we don't specify 3.2.15 we'll end up with sass 3.3.2 in the main
# Gemfile.lock but since sass-rails gets generated (rails new) into the test app
# it'll want sass 3.2.0 and we'll have a conflict
gem 'sass', '~> 3.2.0'
gem 'bootstrap-sass', ">= 3.2"

group :test do
  # Peg simplecov to < 0.8 until this is resolved:
  # https://github.com/colszowka/simplecov/issues/281
  gem 'simplecov', '~> 0.7.1', require: false
  gem 'coveralls', require: false
end

更新

如果有帮助,请添加我的 gem 环境

RubyGems Environment:
  - RUBYGEMS VERSION: 2.4.5
  - RUBY VERSION: 2.2.2 (2015-04-13 patchlevel 95) [x86_64-darwin14]
  - INSTALLATION DIRECTORY: /Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0
  - RUBY EXECUTABLE: /Users/allenlin/.rbenv/versions/2.2.2/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/allenlin/.rbenv/versions/2.2.2/bin
  - SPEC CACHE DIRECTORY: /Users/allenlin/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Users/allenlin/.rbenv/versions/2.2.2/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-14
  - GEM PATHS:
     - /Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0
     - /Users/allenlin/.gem/ruby/2.2.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /Users/allenlin/.rbenv/versions/2.2.2/bin
     - /usr/local/Cellar/rbenv/0.4.0/libexec
     - /Users/allenlin/.rbenv/shims
     - /Applications/Postgres.app/Contents/Versions/9.3/bin
     - /Library/Frameworks/Python.framework/Versions/2.7/bin
     - /Users/allenlin/Library/Enthought/Canopy_64bit/User/bin
     - /Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/bundler-1.9.6/bin
     - /Users/allenlin/gradle-1.12/bin
     - /usr/local/bin
     - /Users/allenlin/apache-maven/apache-maven-3.1.1/bin
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin
     - /opt/X11/bin
     - /usr/local/git/bin
     - /usr/texbin

【问题讨论】:

  • (1)此 Gemfile 包含多个主要来源。 (2)您的 Gemfile 不止一次列出 gem geoblacklight (>= 0)。(3)您必须 gem install bundlerbundle install
  • @pangpang 感谢您的回复!我已经更新了帖子!本质上,我不明白为什么错误和警告消息说我有多个“来源”和 geoblacklight。我还应该查看其他文件吗?
  • 当您按照以下说明操作时发生了什么:“(通过使用 --trace 运行任务查看完整跟踪)”? :)
  • 另外 - 底部的整个文件扩展路径位。这不是第二次加载相同的 Gemfile 吗?这可能就是为什么你会得到“两个来源”和其他两个来源
  • @TarynEast 感谢您提醒我! :) 请查看完整的跟踪记录!

标签: ruby rspec rubygems rake


【解决方案1】:

之前运行后:sudo gem install bundler

我创建了一个 Gemfile:

gem 'rake'
gem 'rspec'
gem 'kramdown'

并且做了bundle install。现在我没有得到错误。

【讨论】:

    【解决方案2】:

    我遇到了这个问题,因为在我的 gem 文件中我没有指定 rspec 的版本并且它得到了旧版本。 我更新了我的 gem 文件:

    gem 'rspec', '3.8.0'

    它成功了!

    【讨论】:

    • 果然gem install rspec修好了。
    【解决方案3】:

    似乎运行bundle exec rake -t 会起作用。每个 bundler.io http://bundler.io/man/bundle-exec.1.html 我建议添加 bundle exec 以确保我们使用当前上下文中指定的 Gem。另请参阅此 SO 帖子。 What does bundle exec rake mean?

    【讨论】:

    • -t 只是添加一个跟踪
    【解决方案4】:

    在生产环境中

    发生的情况是,capistrano 被配置为仅在 production 组中安装 gem 包(如在 Gemfile 中)。所以它运行像bundle install --without development test这样的捆绑命令。

    要验证吗?

    在生产环境的项目目录中运行bundle install命令,你会看到如下

    Using devise 4.2.0
    Using activeadmin 1.0.0.pre4
    Using responsive_active_admin 0.0.5
    Bundle complete! 60 Gemfile dependencies, 133 gems now installed.
    Gems in the groups development and test were not installed.
    Bundled gems are installed into /path/to/your/app/production/shared/bundle.
    

    请注意(开发和测试组中的 Gems 未安装。)。

    您可能还记得您已指定rspec-rails gem 仅安装在development 模式下。


    解决方案

    尝试指定RAILS_ENVlike

    # For Rails 5.0 +
    RAILS_ENV=production bundle exec rails -T
    
    # or 
    
    RAILS_ENV=production bundle exec rake -T
    
    # but this may not work
    bundle exec rake -T RAILS_ENV=production
    

    【讨论】:

      【解决方案5】:

      假设您的 Gemfile 中确实有 rspec,如果您收到此错误,则意味着您没有使用应用程序所需的适当 gem 运行 rake(使用应用程序根目录中的 bundle exec rake 来解决),或者您的 rspec 核心 gem 尚未安装。

      如果在运行bundle install 之后,在运行bundle exec rake 时仍然遇到同样的错误,请检查您是否实际安装了“测试模式”gem。这发生在我身上。

      就我而言,我没有安装我的测试 gem,因为 Bundler 通过记住传递给 --without 选项 as noted here 的先前参数表现出非常规的行为,所以你可能认为你正在安装所有的 gem你运行bundle install,但你实际上可能也在运行(例如......)--without development test,但并不知道。

      检查“未安装开发和测试组中的 Gems”之类的内容。在bundle install 输出的末尾。

      要重置您的捆绑选项,只需删除您的./.bundle 目录(所以...rm -r ./.bundle)。现在bundle install 将恢复正常,rake 应该可以工作了。

      【讨论】: