Question

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'
Was it helpful?

Solution

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 the application.rb for whatever reason.

Adding

config.autoload_paths += %W(#{config.root}/lib)

in my Application fixed the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top