# Controller test - setting session values not working

**URL:** <https://www.ruby-forum.com/t/controller-test-setting-session-values-not-working/131733>\
**Category:** Rails\
**Created:** [March 12, 2008, 8:22pm UTC](https://www.ruby-forum.com/t/controller-test-setting-session-values-not-working/131733 "2008-03-12T20:22:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 12, 2008, 8:22pm UTC](https://www.ruby-forum.com/t/controller-test-setting-session-values-not-working/131733/1 "2008-03-12T20:22:22Z")

</div>

I have a functional test and am trying to set a session value

…  
def test\_should\_check\_number\_of\_jobs  
session[:employee\_id] = 1 #@fred.id  
post :index  
assert\_equal 1, assigns(:jobs).size  
end  
…

when I try to run this test I get the following error  
TypeError: Symbol as array index  
job\_controller\_test.rb:29in ‘[]=’  
job\_controller\_test.rb:29in ‘test\_should\_check\_number\_of\_jobs’

if I hard code the employee\_id in the controller and remove the session  
call in the test, it works perfectly.

[http://zentest.rubyforge.org/ZenTest/classes/Test/Rails.html](http://zentest.rubyforge.org/ZenTest/classes/Test/Rails.html)  
seems to be doing the same thing as me about 1/2 way down the page, so  
it doesn’t seem too unreasonable. Anyone know why this isn’t working?

---

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 12, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/controller-test-setting-session-values-not-working/131733/2 "2008-03-12T20:36:51Z")

</div>

On Mar 12, 2008, at 3:22 PM, Bob Br wrote:

> I have a functional test and am trying to set a session value
> 
> …  
> def test\_should\_check\_number\_of\_jobs  
> session[:employee\_id] = 1 #@fred.id  
> @request.session[:employee\_id] = 1
> 
> if I hard code the employee\_id in the controller and remove the  
> session  
> call in the test, it works perfectly.
> 
> [http://zentest.rubyforge.org/ZenTest/classes/Test/Rails.html](http://zentest.rubyforge.org/ZenTest/classes/Test/Rails.html)  
> seems to be doing the same thing as me about 1/2 way down the page, so  
> it doesn’t seem too unreasonable. Anyone know why this isn’t working?

The request/controller/response is meant to be a sufficient stand-in  
for the real things, but remember that you’re not running in the  
context of the controller where the session method would normally be  
found.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 12, 2008, 9:13pm UTC](https://www.ruby-forum.com/t/controller-test-setting-session-values-not-working/131733/3 "2008-03-12T21:13:40Z")

</div>

You guys rock!

Thanks for the help

---

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [July 17, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/controller-test-setting-session-values-not-working/131733/4 "2008-07-17T13:42:44Z")

</div>

Hey Bob,

I am a Rails newbie and facing a stupid error. Thought you could help.

I am testing a functionality and want a value to persist. I open my  
application.rb and try to store the value as: session[:business\_id] =

1. When I access my application, I get an error: Symbol as array index

Any idea why?

Thanks in advance,  
–Vaibhav

---

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [July 17, 2008, 10:59pm UTC](https://www.ruby-forum.com/t/controller-test-setting-session-values-not-working/131733/5 "2008-07-17T22:59:35Z")

</div>

Thats what Rob was talking about, you need to add @request, so you wind  
up with  
@request.session[:employee\_id] = 1

I usually initalize a bunch of this stuff in the setup function, and  
then later on accessing it is like normal without the @request.

Hope this helps, let me know if you need anything else.

Bob

Vaibhav wrote:

> Hey Bob,
> 
> I am a Rails newbie and facing a stupid error. Thought you could help.
> 
> I am testing a functionality and want a value to persist. I open my  
> application.rb and try to store the value as: session[:business\_id] =
> 
> 1. When I access my application, I get an error: Symbol as array index
> 
> Any idea why?
> 
> Thanks in advance,  
> –Vaibhav
