Andrew Cox
Andrew Cox

Reputation: 165

Load up the lib files when running Test::Unit tests in Rails?

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

Upvotes: 4

Views: 3132

Answers (1)

Ben Hughes
Ben Hughes

Reputation: 14195

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.

Upvotes: 4

Related Questions