RSpec 1.x 至 RSpec 2.x(Rails 2.x 至 Rails 3.x)
我正在将 Rails 2.x 应用程序转换为 Rails 3.x。 Rails 2.x 项目的规范当前已全部通过。
然而,将规范移至 Rails 3.x 应用程序后,规范消失了,无法找到 Rails.root + '/lib'< 中定义的基类 (
SmsCommand::Base
) /代码> 目录。我尝试仅使用 rspec path_to_spec
、rake spec
和 bundle exec rspec path_to_spec
运行规范,但均无济于事。
我担心这可能与目录嵌套有关。举个例子:
/spec/models/sms_commands/accept_spec.rb
是规范:
/app/models/sms_commands/accept.rb
Accept
类继承自 SmsCommand::Base
,它包含在:
/lib/sms_command.rb
似乎 Rails 自动加载器没有发生在规范中或者它根本不自动加载 /lib 目录。
rake spec 的输出是:
/Users/xxx/.rvm/gems/ruby-1.9.2-p180@a_project/gems/rspec-core 2.5.1/lib/rspec/core/backward_compatibility.rb:20:
in `const_missing': uninitialized constant Object::SmsCommand (NameError)
from /Users/xxx/Projects/a_project/app/models/sms_commands/accept.rb:2:in `<top (required)>'
每个规范所需的 My spec_helper
具有以下相关行:(我尝试使用手动 require 来强制解决该问题
对于 lib
目录中的每个 *.rb
。)
ENV["RAILS_ENV"] ||= 'test'
Dir[File.expand_path(File.join(File.dirname(__FILE__),'..', 'lib','**','*.rb'))].each {|f| require f}
require File.expand_path(File.join(File.dirname(__FILE__),'..','config','environment'))
#require 'spec/autorun'
require 'spec/rails'
I am in the process of converting a Rails 2.x application to Rails 3.x. The Rails 2.x project's specs execute with all passing currently.
However after moving the specs to the Rails 3.x application the specs die, unable to find a base class (SmsCommand::Base
) defined in the Rails.root + '/lib'
directory. I have tried running the specs with just rspec path_to_spec
, rake spec
and bundle exec rspec path_to_spec
all to no avail.
I am concerned that it may have to do with directory nesting. As an example:
/spec/models/sms_commands/accept_spec.rb
is the spec for:
/app/models/sms_commands/accept.rb
The Accept
class inherits from SmsCommand::Base
which is contained at:
/lib/sms_command.rb
It seems as though the Rails autoloader isn't happening for specs or its just not autoloading the /lib directory at all.
The output from rake spec
is:
/Users/xxx/.rvm/gems/ruby-1.9.2-p180@a_project/gems/rspec-core 2.5.1/lib/rspec/core/backward_compatibility.rb:20:
in `const_missing': uninitialized constant Object::SmsCommand (NameError)
from /Users/xxx/Projects/a_project/app/models/sms_commands/accept.rb:2:in `<top (required)>'
My spec_helper
which is required by each spec has the following pertinent lines: (I have tried to force the issue with the manual require
for each *.rb
in the lib
directory.)
ENV["RAILS_ENV"] ||= 'test'
Dir[File.expand_path(File.join(File.dirname(__FILE__),'..', 'lib','**','*.rb'))].each {|f| require f}
require File.expand_path(File.join(File.dirname(__FILE__),'..','config','environment'))
#require 'spec/autorun'
require 'spec/rails'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读本文:
https ://rails.lighthouseapp.com/projects/8994/tickets/5218-rails-3-rc-does-not-autoload-from-lib
...事实证明
/lib 默认情况下不会自动加载,并且无论出于何种原因,在
application.rb
中实际上都没有警告。添加
我的
Application
解决了问题。From reading this:
https://rails.lighthouseapp.com/projects/8994/tickets/5218-rails-3-rc-does-not-autoload-from-lib
... it turns out that
/lib
isn't autoloaded by default and there's really no warning in theapplication.rb
for whatever reason.Adding
in my
Application
fixed the problem.