# HashWithIndifferentAccess in testing

**URL:** <https://www.ruby-forum.com/t/hashwithindifferentaccess-in-testing/152072>\
**Category:** Rails\
**Created:** [October 29, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-in-testing/152072 "2008-10-29T20:17:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [October 29, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-in-testing/152072/1 "2008-10-29T20:17:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [October 29, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-in-testing/152072/2 "2008-10-29T20:40:30Z")

</div>

Hi,

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

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [October 29, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-in-testing/152072/3 "2008-10-29T20:43:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [October 29, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-in-testing/152072/4 "2008-10-29T20:47:35Z")

</div>

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

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

#my stuff  
APP\_TEXT = …

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [October 29, 2008, 9:00pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-in-testing/152072/5 "2008-10-29T21:00:29Z")

</div>

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](mailto: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)

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [October 29, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-in-testing/152072/6 "2008-10-29T20:54:38Z")

</div>

On Oct 29, 7:46 pm, reHa [removed\_email\_address@domain.invalid](mailto: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)

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [October 29, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-in-testing/152072/7 "2008-10-29T21:19:55Z")

</div>

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