0

config/initializers/aws_config.rb:

AWS_CONFIG = YAML.load_file("#{::Rails.root}/config/aws.yml")[Rails.env]

Model has code:

self.bucket = AWS_CONFIG["bucket"]

Test result:

 Failure/Error: self.bucket = AWS_CONFIG['bucket']
 NoMethodError:
   undefined method `[]' for nil:NilClass

There's no problem in development or production running the code, why does the aws_config initializer fail under RSpec?

  • When you open your `rails console` what does the AWS_CONFIG give you? My guess is that the `test` key is not set/properly in the config file. kindly confirm this! – oreoluwa Jun 08 '16 at 22:33
  • Can you paste your aws.yml file just hide the was credentials – Piyush Patil Jun 08 '16 at 22:38
  • rails console works fine, AWS_CONFIG['bucket'] is setup with the correct value. I will add that this is a rails engine I'm testing. The aws.yml file is basically what is posted in the answer below. – Keith Johnson Jun 09 '16 at 02:42

2 Answers2

0

My guess is that the 'test' is not set/properly in your configuration file. As an added bonus, when loading your configuration files from config/*.yml, Rails provides a helper method to fetch the configurations. Assuming you've nested your configurations appropriately, you should be able to #{your app name here(find it in config/application.rb)}::Application.config_for(:aws), if you've set it properly, you should just be able to fetch the values, without looking under the environment.

Take a look at this for example

production:
  secret_key: 000894jnjfbdfu39
  secret_id: 123456
  bucket: shadow

development:
  secret_key: 000894jnjfbdfu39
  secret_id: 123456
  bucket: shadow

test:
  secret_key: 000894jnjfbdfu39
  secret_id: 123456
  bucket: meadow

In your application initializer, you could then have:

AWS_CONFIG = YourApp::Application.config_for(:aws) All other things should work normally after this.

oreoluwa
  • 5,553
  • 2
  • 20
  • 27
0

engine.rb did not require gems.

this is why model didn't have AWS_CONFIG, while rails console did.

See, this answer, for how to load gems in engine.

Community
  • 1
  • 1