简体   繁体   English

当类存在于子目录中并且是子类时,Rspec与load_missing_constant失败

[英]Rspec failing with load_missing_constant when classes live in subdirectories and are subclassed

I'm having some troubles running a bunch of RSpec tests due to something I suspect is related to autoloading. 由于我怀疑与自动加载相关的东西,我在运行一堆RSpec测试时遇到了一些麻烦。 Here is the error: 这是错误:

/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)>'

Here is the header of the file: 这是文件的标题:

class Portfolios::BaseManageController < ApplicationController

And it lives in app/controllers/portfolios/base_manage_controller.rb 它存在于app/controllers/portfolios/base_manage_controller.rb

And its subclass: 它的子类:

class Portfolios::CustomizeController < Portfolios::BaseManageController

And it lives in app/controllers/portfolios/customize_controller.rb 它存在于app/controllers/portfolios/customize_controller.rb

Finally here are a bunch of autoloads: 最后这里有一堆自动加载:

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

Any help would be much appreciated! 任何帮助将非常感激!

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. 在这种情况下,尝试定义base_manage_controller.rb中找到的类时可能会发生运行时错误。

To verify this is the case, try stripping out everything from base_manage_controller.rb , except the class declaration: 要验证这种情况,请尝试从base_manage_controller.rb删除所有base_manage_controller.rb ,但类声明除外:

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 . 要查找运行时错误,请将所有内容放回到类中,然后通过调用Portfolios::BaseManageControllerscript/console加载它。 That will attempt to dynamically load the class, and raise the exception that prevents your class from being defined. 这将尝试动态加载类,并引发阻止您的类被定义的异常。

It looks like that Portfolios is not defined when rspec is loading Portfolios::BaseManageController . 它看起来像Portfolios时没有定义rspec加载Portfolios::BaseManageController Where did you define Portfolios ? 您在哪里定义Portfolios If it's defined as a module or class somewhere in a separate file, you need to require this file first. 如果它在单独的文件中的某个位置定义为模块或类,则需要首先require此文件。

there is a collision in your autoload path because of the existence of a file with the same name at /var/lib/jenkins/.../portfolios/base_manage_controller.rb 由于/var/lib/jenkins/.../portfolios/base_manage_controller.rb中存在同名文件,因此自动加载路径中存在冲突

You need to either modify your load path so app/controllers comes up earlier in the list or the file specified at the path above should be renamed or removed. 您需要修改加载路径,以便app / controllers在列表中较早出现,或者应重命名或删除上述路径中指定的文件。

Following solution will help you 以下解决方案将帮助您

create spec in following path for app/controllers/portfolios/base_manage_controller.rb spec/controllers/portfolios/base_manage_controller_spec.rb 在app / controllers / portfolio / base_manage_controller.rb spec / controllers / portfolio / base_manage_controller_spec.rb的以下路径中创建规范

The spec content should be like this 规范内容应该是这样的

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

You can apply the same logic for app/controllers/portfolios/customize_controller.rb 您可以为app / controllers / portfolio / customize_controller.rb应用相同的逻辑

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RSpec load_missing_constant,期望X定义Y(并且确实如此) - RSpec load_missing_constant, expected X to define Y (and it does) 尝试在Rails上创建观察者时出现“ load_missing_constant” - 'load_missing_constant' when trying to create an observer on Rails `block in load_missing_constant':启动 Rails 服务器和控制台时未初始化的常量 Pry::Command::ExitAll (NameError) - `block in load_missing_constant': uninitialized constant Pry::Command::ExitAll (NameError) when launching rails server and console `load_missing_constant&#39;:未初始化的常量AuthenticatedTestHelper(NameError) - `load_missing_constant': uninitialized constant AuthenticatedTestHelper (NameError) `load_missing_constant&#39;:未初始化的常量Rails :: Railtie - `load_missing_constant': uninitialized constant Rails::Railtie radrails开发服务器启动错误:load_missing_constant - radrails dev server startup error: load_missing_constant 安装Typus rails插件后的`load_missing_constant&#39; - `load_missing_constant' after installing Typus rails plugin 模块`ActiveSupport :: Dependencies&#39;的未定义方法`load_missing_constant&#39; - undefined method `load_missing_constant' for module `ActiveSupport::Dependencies' load_missing_constant&#39; 中的块:未初始化的常量 API::V1::Users (NameError) - block in load_missing_constant': uninitialized constant API::V1::Users (NameError) `load_missing_constant':在自动加载常量 ApplicationRecord (RuntimeError) 时检测到循环依赖 - `load_missing_constant': Circular dependency detected while autoloading constant ApplicationRecord (RuntimeError)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM