11

由于我怀疑与自动加载有关的事情,我在运行一堆 RSpec 测试时遇到了一些麻烦。这是错误:

/usr/share/ruby-rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:490:in `load_missing_constant': Expected /var/lib/jenkins/.../portfolios/base_manage_controller.rb to define Portfolios::BaseManageController (LoadError)
from /usr/share/ruby-rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:181:in `block in const_missing'
from /usr/share/ruby-rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:179:in `each'
from /usr/share/ruby-rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:179:in `const_missing'
from /usr/share/ruby-rvm/gems/ruby-1.9.2-p320@global/gems/rake-0.9.2.2/lib/rake/ext/module.rb:36:in `const_missing'
from /var/lib/jenkins/jobs/.../app/controllers/portfolios/customize_controller.rb:1:in `<top (required)>'

这是文件的标题:

class Portfolios::BaseManageController < ApplicationController

它住在app/controllers/portfolios/base_manage_controller.rb

及其子类:

class Portfolios::CustomizeController < Portfolios::BaseManageController

它住在app/controllers/portfolios/customize_controller.rb

最后这里有一堆自动加载:

config.autoload_paths += Dir["#{config.root}/lib",  "#{config.root}/lib/**/"]
config.autoload_paths += %W(#{config.root}/app/models/statistics)

#Any test/dev specific load paths
if not Rails.env.production?
  config.autoload_paths += %W(#{config.root}/spec/support)
  config.autoload_paths += %W(#{config.root}/spec/support/builders)
  config.autoload_paths += %W(#{config.root}/spec/support/modules)
  config.autoload_paths += %W(#{config.root}/spec/support/utils)
end

任何帮助将非常感激!

4

4 回答 4

2

This error sometimes occurs when there is an exception raised when the class is defined. In this case, a runtime error may be occurring when attempting to define the class found in base_manage_controller.rb.

To verify this is the case, try stripping out everything from base_manage_controller.rb, except the class declaration:

class Portfolios::BaseManageController < ApplicationController
end

It should result in the specs running, but failing.

To find the runtime error, put everything back into the class, and load it from script/console, by calling Portfolios::BaseManageController. That will attempt to dynamically load the class, and raise the exception that prevents your class from being defined.

于 2012-12-15T11:16:23.107 回答
0

加载时似乎Portfolios没有定义。你在哪里定义的?如果它被定义为单独文件中某处的模块或类,则需要首先使用此文件。rspecPortfolios::BaseManageControllerPortfoliosrequire

于 2012-08-30T09:26:32.750 回答
0

由于 /var/lib/jenkins/.../portfolios/base_manage_controller.rb 中存在同名文件,您的自动加载路径中存在冲突

您需要修改加载路径,以便 app/controllers 出现在列表的前面,或者应该重命名或删除在上面路径中指定的文件。

于 2012-09-30T19:09:08.203 回答
0

以下解决方案将为您提供帮助

在以下路径中为 app/controllers/portfolios/base_manage_controller.rb 创建 规范 spec/controllers/portfolios/base_manage_controller_spec.rb

规范内容应该是这样的

describe Portfolios::BaseManageController do
 -----
end

您可以对 app/controllers/portfolios/customize_controller.rb 应用相同的逻辑

于 2012-12-03T07:08:06.943 回答