1
votes

I am just learning Rails 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?

2
How does your config file looks like?Deradon
development: upgrade_price: 24 root_url: localhost:3000 # etc test: upgrade_price: 24 root_url: localhost:3000 # etcuser478798

2 Answers

3
votes

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)

0
votes

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