在test.rb中加载的变量在测试中找不到
我正在运行rspec 2.5.1,ruby 1.9.2和rails 3.0.5
我移动了一些设置,用于将邮件发送到yaml文件中,我在environment.rb中加载:
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")
邮件程序类是这样的:
class Notifier APP_CONFIG['support_email'] ... end
这在开发中效果很好,但是在运行任何测试之前,rspec会咳嗽一个毛球:
/.../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 ''
我没有运行spork或类似的东西,所以我认为必须加载rails环境才能运行测试? 任何帮助弄清楚我搞砸了什么都会很棒。
如果我发布代码的任何其他部分,请在评论中告诉我,谢谢。
我经常明确地将常量定义为全局常量,以便在我想要区分时它们不是命名空间。 这通常有助于澄清这些类型的问题。
::APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml") class Notifier < ActionMailer::Base default :from => ::APP_CONFIG['support_email'] ... end
您可能还应该将该APP_CONFIG定义移动到application.rb文件中,而不是Rails 3中的environment.rb文件。