测试中未找到 environment.rb 中加载的变量 [英] Variable loaded in environment.rb not found in tests
问题描述
我正在运行 rspec 2.5.1、ruby 1.9.2 和 rails 3.0.5
I am running rspec 2.5.1, ruby 1.9.2, and rails 3.0.5
我将一些用于发送邮件的设置移到了我在 environment.rb 中加载的 yaml 文件中:
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")
邮件程序类是这样的:
class Notifier < ActionMailer::Base
default :from => APP_CONFIG['support_email']
...
end
这在开发中效果很好,但在运行任何测试之前 rspec 会吐出一团毛球:
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>'
我没有运行 spork 或类似的东西,所以我认为必须加载 rails 环境才能运行测试?任何帮助弄清楚我搞砸了什么都会很棒.
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.
推荐答案
当我想区分时,我经常将常量明确定义为全局常量,以便它们没有命名空间.这通常有助于澄清这些类型的问题.
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
您可能还应该将该 APP_CONFIG 定义移动到 application.rb 文件中,而不是 Rails 3 中的 environment.rb 文件中.
You should probably also move that APP_CONFIG definition into the application.rb file instead of the environment.rb file in Rails 3.
这篇关于测试中未找到 environment.rb 中加载的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!