2011-03-12 47 views
1

我运行rspec的2.5.1,1.9.2红宝石没有找到,并且轨道3.0.5变量environment.rb中加载测试

我提出我的一些设置,发送邮件到YAML文件,我负载environment.rb中:

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

的邮件类是这样的:

class Notifier < ActionMailer::Base 
    default :from => 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 '<class:Notifier>' 

我没有运行spork或类似的东西,所以我认为rails环境不得不加载测试才能运行?任何帮助搞清楚我搞砸了会很好。

如果我应该发布代码的任何其他部分让我知道在评论中,谢谢。

回答

2

我会经常显式地将常量定义为全局常量,以便在我想进行区分时不会命名空间。这通常有助于澄清这些类型的问题。

::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

+0

environment.rb文件中它的工作原理,谢谢。 – blu 2011-03-12 21:11:57