我正在将带有 Rspec 的测试集成到一个相当大的/已开发的应用程序中。当我运行测试时,我的一些初始化程序出现了非常奇怪的错误。
例如,我的 carrierwave.rb
加载了一个 yaml 文件,当我运行测试命令时,我得到:
carrierwave.rb:11:in `block in <top (required)>': undefined method `[]' for nil:NilClass
指的是下面第二行代码:
fog_config = YAML::load_file(Rails.root.join 'config/fog.yml')[Rails.env.to_s]
config.fog_directory = fog_config['directory']
如果我暂时删除 carrierwave 中的那行代码,omniauth 也会开始提示:
omniauth.rb:4:in `block in <top (required)>': undefined method `symbolize_keys' for nil:NilClass (NoMethodError)
同样,另一个 yaml 文件:
fb_config = YAML::load_file(Rails.root.join 'config/fb_app_version.yml')[Rails.env].symbolize_keys
我猜解决方案是更改我的 yaml 文件或在测试中包含一些内容,但我不确定是什么。任何想法将不胜感激,谢谢。
最佳答案
看起来您需要在相关 Yaml 文件中包含 test
条目。
例如,您的 config/fog.yml
可能看起来像这样:
production:
directory: the_prod_directory
other_keys: ...
development:
directory: the_dev_directory
other_keys: ...
您需要添加一个测试
键:
test:
directory: the_test_dir
other_keys: appropriate values
关于ruby - 由于初始化程序 Yaml 文件,Rspec 测试未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16022591/