开发者

Rails testing not loading custom app config file

I am just learning Rail开发者_开发知识库s testing and I am not able to run ANY tests (even the "assert true" examples) because I get an error from an unrelated model not being able to load a custom config file variable.

My app has a custom config file loaded per this Railscast episode (http://railscasts.com/episodes/85-yaml-configuration-file) in environment.rb

APP_CONFIG = YAML.load_file("#{::Rails.root.to_s}/config/config.yml")[RAILS_ENV]

Thats fine in the normal app but in testing it is not loading it in a model:

    has_attached_file :photo,
                      :bucket => APP_CONFIG['s3_bucket']

Console error: `const_missing_from_s3_library': uninitialized constant Asset::APP_CONFIG (NameError)

(It should be noted that is NOT the same model I am trying to test but I guess the testing environment runs the whole app?)

What is different about the testing environment where it won't run environment.rb?


I figured this out myself because Heroku and the testing environment have one thing in common:

Heroku does NOT load environment.rb before the models and neither does the testing environment.

All my controllers loaded the custom config vars mentioned above fine (in dev and Heroku) but as soon as a model tried to load one of those in Heroku or testing, it freaked out.

Lesson: for Heroku, use their custom config vars and reference it by ENV['name'] and add 'export name="whatever"' to your .bash_profile

(Or attempt this)


I put mine at the bottom of application.rb (rails 3.2) and it worked fine on Heroku.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜