ruby-on-rails-3rspecrspec2ruby-on-rails-3.1spork

"Uninitialized constant" error when using RSpec in a Rails 3.1 project


I'm trying to use RSpec to test a Rails 3.1rc4 project. I want to test the code I have in my lib/ directory. I'm getting:

/home/julas/myapp/spec/lib/operation_spec.rb:3:in `<top (required)>': uninitialized constant MyApp (NameError)

MyApp is a module (namespace) and the problem appears whenever I try to use something from this namespace, e.g. when I try to do

describe MyApp::Something do

in my test.

The test worked without any problems earlier. I load all the stuff form the lib/ to the Rails environment by adding config.autoload_paths += Dir["#{config.root}/lib/**/"] to application.rb.

I'm really clueless what causes this. Anyone experienced something similar?

Update: it seems it wasn't related with Spork. I must have missed something.


Solution

  • I have no idea why this worked earlier, but it seems that to test the code from the lib directory you have to require it in the spec file (it is not auto-loaded like in e.g. controllers). Source