blu
blu

Reputation: 13175

Variable loaded in environment.rb not found in tests

I am running rspec 2.5.1, ruby 1.9.2, and rails 3.0.5

I moved some of my settings for sending mail into a yaml file which I load in environment.rb:

APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")

The mailer class is something like this:

class Notifier < ActionMailer::Base
  default :from => APP_CONFIG['support_email']
  ...
end

This works great in dev, but rspec coughs up a hairball before running any tests:

/.../rspec/core/backward_compatibility.rb:20:in
  'const_missing': uninitialized constant Notifier::APP_CONFIG (NameError) 
    from /rspec/expectations/backward_compatibility.rb:6:in 'const_missing'     
    from /.../app/mailers/notifier.rb:2:in '<class:Notifier>'

I am not running spork or anything like that, so I thought the rails environment had to be loaded for the tests to run? Any help figuring out what I messed up would be great.

If I should post any other parts of the code let me know in the comments, thanks.

Upvotes: 1

Views: 1273

Answers (1)

Pan Thomakos
Pan Thomakos

Reputation: 34350

I'll often explicitly define constants as global constants, so that they aren't namespaced, when I want to make the distinction. This usually helps clarify these types of issues.

::APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")

class Notifier < ActionMailer::Base
  default :from => ::APP_CONFIG['support_email']
  ...
end

You should probably also move that APP_CONFIG definition into the application.rb file instead of the environment.rb file in Rails 3.

Upvotes: 2

Related Questions