When I assign in my controller
@my_hash = { :my_key => :my_value }
and test that controller by doing
get 'index'
assigns(:my_hash).should == { :my_key => :my_value }
then I get the following error message:
expected: {:my_key=>:my_value},
got: {"my_key"=>:my_value} (using ==)
Why does this automatic symbol to string conversion happen? Why does it affect the key of the hash?
It may end up as a
HashWithIndifferentAccessif Rails somehow gets ahold of it, and that uses string keys internally. You might want to verify the class is the same:Parameters are always processed as the indifferent access kind of hash so you can retrieve using either string or symbol. If you’re assigning this to your params hash on the
getorpostcall, or you might be getting converted.Another thing you can do is freeze it and see if anyone attempts to modify it because that should throw an exception: