views:

891

answers:

1

I've added a few modules and dropped them in my /lib directory and I think the lib directory is loaded magically by Rails (unless I loaded the lib directory somewhere early in my project and forgot about it). However, when I run unit tests that require my additional modules, they are not loaded.

Should the lib directory be loaded automatically when running tests, or is there an elegant way to do so for testing? I had hoped that the rake scripts + Test::Unit would've loaded up my Rails environment exactly, but this doesn't seem to be the case. I'm left with doing adding something like this to test_helper.rb:

require File.expand_path(File.dirname(__FILE__) + "/../lib/foo")

I'm running my tests with the standard rake scripts like:

rake test
rake test:units
rake test:functionals
+3  A: 

Your lib directory is not automatically loaded by rails. Loading occurs through ActiveSupport::Dependencies overriding const_missing. When you use a constant for the first time, if it is undefined, Rails attempts to find it in the lib directory (and other places in the load path). To accomplish this, it uses a naming scheme where something called SomeClass is expected to be in some_class.rb. Rails in test mode uses the same mechanism. Check your config/environments/test.rb and config/environments/development.rb to see if you do something funny with requires. In short, check your naming scheme.

Ben Hughes
You're right - turns out I was doing my require with a relative path instead of the constant like you said, so I had: require 'lib/rexml'Instead of just: require 'rexml'Now everything is working fine in my tests without having to explicitly load it in my text_helper.
Andrew Cox