HashWithIndifferentAccess in testing


#1

Hi,
I’m trying to externalize the strings in my application, and have this
in the environment.rb file
APP_TEXT =
(HashWithIndifferentAccess).new(YAML::load(File.open("#{RAILS_ROOT}/config/strings.yml"))),
This works fine until I try to do the functional and unit tests, where I
get an uninitialized constant HashWithIndifferentAccess (NameError).
I’ve tried it with Hash.HashWithIndifferentAccess,
Hash:HashWithIndifferentAccess, and Hash::HashWithIndifferentAccess and
none of these work.
Am I supposed to be including something somewhere inside of the tests?


#2

Hi,

Do you put this line APP_TEXT = …
at the end or beginning of the environment.rb file?


#3

First I put it right below
require File.join(File.dirname(FILE), ‘boot’)
and then I tried moving it to
Rails::Initializer.run do |config|
APP_TEXT =
(HashWithIndifferentAccess).new(YAML::load(File.open("#{RAILS_ROOT}/config/strings.yml")))
however this does not work.

reHa wrote:

Hi,

Do you put this line APP_TEXT = …
at the end or beginning of the environment.rb file?


#4

I ussually add my things after the initializer - mybe try this way

Rails::Initializer.run do |config|
#standard stuff
end

#my stuff
APP_TEXT = …


#5

Thanks! that worked perfectly,
I still don’t really understand why the error didn’t come up during
development and only during the unit testing. But thanks a lot!

Frederick C. wrote:

On Oct 29, 7:46�pm, reHa removed_email_address@domain.invalid wrote:

I ussually add my things after the initializer - mybe try this way

Rails::Initializer.run do |config|
� #standard stuff
end

#my stuff
APP_TEXT = …

The reason why this is a good thing is that it means the code runs
after Rails has been loaded. Given that HashWithIndifferentAccess is
part of rails this is a good thing !

Fred
(PS: config/initializers)


#6

On Oct 29, 7:46 pm, reHa removed_email_address@domain.invalid wrote:

I ussually add my things after the initializer - mybe try this way

Rails::Initializer.run do |config|
#standard stuff
end

#my stuff
APP_TEXT = …

The reason why this is a good thing is that it means the code runs
after Rails has been loaded. Given that HashWithIndifferentAccess is
part of rails this is a good thing !

Fred
(PS: config/initializers)


#7

No problem - actually it is interisting question why it worked on the
development environment but I don’t know the answer :wink: