NoMethodError: undefined method `last_comment' after upgrading to rake 11

NoMethodError: undefined method `last_comment' after upgrading to rake 11

当 运行 任何 rake 任务时,我得到:

NoMethodError: undefined method `last_comment' for

这是在 bundle update 引入新版 rake 之后,版本 11.0.1

$ grep rake Gemfile.lock
       rake
       rake (>= 0.8.7)
     rake (11.0.1)
       rake
$ bundle update
$ bundle exec rake db:drop # any rake task

NoMethodError: undefined method `last_comment' for #< Rake::Application:0x007ff0cf37be38>

版本

Rake 11.0.1 removes last_comment 方法 Rails 2.3 rspec-core (< 3.4.4 ) 使用。因此 until/if 发布了一个补丁,我们需要将 rake 固定到 Gemfile 中的旧版本:

gem 'rake', '< 11.0'

然后:

$ bundle update
$ grep rake Gemfile.lock 
      rake
      rake (>= 0.8.7)
    rake (10.5.0)
      rake
  rake (< 11.0)

我们现在使用 rake 10.5.0,它仍然具有 last_comment 方法,我们的 rake 任务将再次运行。

更新:这已在 rspec 中修复,因此唯一需要更新的是 rspec.

在 Rails 中可以编辑快速修复 ./Rakefile(在您的应用程序文件夹中)

并在调用 Rails.application.load_tasks 之前添加这些行:

module TempFixForRakeLastComment
  def last_comment
    last_description
  end 
end
Rake::Application.send :include, TempFixForRakeLastComment

所以整个 Rakefile 可能看起来像

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

+ # temp fix for NoMethodError: undefined method `last_comment'
+ # remove when fixed in Rake 11.x
+ module TempFixForRakeLastComment
+   def last_comment
+     last_description
+   end 
+ end
+ Rake::Application.send :include, TempFixForRakeLastComment
+ ### end of temfix
+ 
  task "resque:preload" => :environment

  Rails.application.load_tasks

这是一个已经解决的 issue in rake

@equivalent8 的回答是一个猴子补丁,应该避免。

正如@Kris 指出的那样,这是一个孤立于 rake 11.0.1 的问题。由于@Kris 已经发布了他的答案,因此有新版本的 Rake 可用,理想情况下,您将能够与时俱进,而不会被固定在旧版本的 rake 上。相信我,我去过那里,如果你能帮忙的话,这不是一个好主意。这也不是 Rails 2.3 或任何版本的 rails.

的问题

任何 Rake < v11.0.1> v11.0.1 and < v12 都可以,但这仍然是一种解决方法,也应该避免;理想情况下,您将能够与时俱进。

由于 last_comment 已弃用,因此应升级依赖项本身。在我的例子中,它是 rspec-core,顺便说一句,它只在 v3.4.4.

中解决了这个问题

修复

将您的依赖项升级到不调用 last_comment 而是调用 last_description 的版本。它可能 rspec 并将 rspec-core 升级到 3.4.4 或更高版本将修复它。 rspec-core < 3.4.4 调用 last_comment.

如果你的依赖没有不调用 last_description 的版本,做个好公民并提交 PR 来修复它:)

更新到最新 Rspec gem 完成工作:

bundle update rspec-rails

只需升级 gem rspec-rails

现在:gem 'rspec-rails', '~> 3.5', '>= 3.5.2'

拥抱!