# Setting session data within Integration Test

**URL:** <https://www.ruby-forum.com/t/setting-session-data-within-integration-test/110779>\
**Category:** Rails\
**Created:** [July 30, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/setting-session-data-within-integration-test/110779 "2007-07-30T20:17:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [July 30, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/setting-session-data-within-integration-test/110779/1 "2007-07-30T20:17:08Z")

</div>

How do I manipulate the session data in an integration test?

We have a controller that manipulates session data, and I want to see,  
passing in certain session data, what comes out.

The session is available in integration tests through module  
ActionController::TestProcess

def session  
@response.session  
end

So if I perform

session[:new\_key] = ‘new\_value’  
session.update

get ‘/some\_controller/some\_action’

assert\_nil session[:new\_key]

this will pass, in other words, it is not possible to manipulate the  
session used for the **next** request in this manner. One can only  
inspect the session

What can I do?

Stephan

---

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [July 31, 2007, 12:14am UTC](https://www.ruby-forum.com/t/setting-session-data-within-integration-test/110779/2 "2007-07-31T00:14:07Z")

</div>

shouldn’t that be @request.session, not @response.session?

On Jul 30, 2:17 pm, Stephan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [July 31, 2007, 12:25am UTC](https://www.ruby-forum.com/t/setting-session-data-within-integration-test/110779/3 "2007-07-31T00:25:49Z")

</div>

Jeff Emminger wrote:

> shouldn’t that be @request.session, not @response.session?

Thanks for your reply, I’m not clear what you are referring to. The  
snippet

def session  
@response.session  
end

that I pasted is from the module, ActionController::TestProcess

Either way, I set @request.session[:new\_key] = ‘new\_value’ and it  
doesn’t survive calling

get ‘/some\_controller/some\_action’

The integration test re-initializes the session object for each request.

Still stuck –

Stephan

---

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [July 31, 2007, 1:57am UTC](https://www.ruby-forum.com/t/setting-session-data-within-integration-test/110779/4 "2007-07-31T01:57:02Z")

</div>

Jeff Emminger wrote:

> ah… i actually haven’t used integration tests yet anyway 😉
> 
> came across this though, seems to deal with simulating multiple users  
> in an integration test… perhaps it will give you some ideas  
> [http://weblog.jamisbuck.org/2006/3/9/integration-testing-in-rails-1-1](http://weblog.jamisbuck.org/2006/3/9/integration-testing-in-rails-1-1)
> 
> On Jul 30, 6:25 pm, Stephan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Well, thanks – they talk about sessions as in “user sessions” ("Bob  
logs in, Bob does this, Stacey logs in, Stacey does that…). Whereas  
I’m looking at the CGI::Session object that is stored on the server  
side, and available as @response.session / @request.session or just  
session.

Stephan

---

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [August 8, 2007, 7:23pm UTC](https://www.ruby-forum.com/t/setting-session-data-within-integration-test/110779/5 "2007-08-08T19:23:31Z")

</div>

I ran into the same problem and never really found a good answer via the  
test objects. The only solution I saw was to actually update the  
session via a controller. I defined a new action in one of my  
controllers from within test\_helper (so the action does not exist when  
actually runnning the application). I also had to create an entry in  
routes. Maybe there’s a better way to update routes while testing.

So from my integration test I can do the following and verfiy:  
assert(session[:fake].nil?, “starts empty”)  
v = ‘Yuck’  
get ‘/user\_session’, :fake =\> v  
assert\_equal(v, session[:fake], “value was set”)

```
assert(session[:foo].nil?, "starts empty")
v_id = '2'
get '/user_session', :foo_id => v_id
assert_equal(v, session[:fake], "previous value still set")
assert(!session[:foo].nil?, , "object entry exists")
assert_equal(v_id.to_i, session[:foo].id, "correct object set")

```

\<test\_helper.rb\>  
ENV[“RAILS\_ENV”] = “test”  
require File.expand\_path(File.dirname( **FILE** ) +  
“/…/config/environment”)  
require ‘test\_help’  
require ‘my\_controller’

# Need to be able to set session values for integration tests

class MyController  
def user\_session  
ignore = [‘action’, ‘controller’]  
params.each do | key, value|  
unless ignore.include?(key)  
if (pos = key.index(’\_id’)).nil?  
session[key.to\_sym] = value  
else  
new\_key = key[0,pos]  
find\_class = new\_key.classify.constantize  
session[new\_key.to\_sym] = find\_class.find(value.to\_i)  
end  
end  
end  
redirect\_to “/”  
end  
end

class Test::Unit::TestCase

Added to routes.rb:  
map.user\_session ‘user\_session’, :controller =\> ‘my’, :action =\>  
‘user\_session’

---

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [July 31, 2007, 1:27am UTC](https://www.ruby-forum.com/t/setting-session-data-within-integration-test/110779/6 "2007-07-31T01:27:12Z")

</div>

ah… i actually haven’t used integration tests yet anyway 😉

came across this though, seems to deal with simulating multiple users  
in an integration test… perhaps it will give you some ideas  
[http://weblog.jamisbuck.org/2006/3/9/integration-testing-in-rails-1-1](http://weblog.jamisbuck.org/2006/3/9/integration-testing-in-rails-1-1)

On Jul 30, 6:25 pm, Stephan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [August 8, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/setting-session-data-within-integration-test/110779/7 "2007-08-08T19:55:36Z")

</div>

Gary W. wrote:

> I ran into the same problem and never really found a good answer via the  
> test objects. The only solution I saw was to actually update the  
> session via a controller. I defined a new action in one of my  
> controllers from within test\_helper (so the action does not exist when  
> actually runnning the application). I also had to create an entry in  
> routes. Maybe there’s a better way to update routes while testing.

Yes, I took the same route, although it seems wrong.

I also couldn’t figure out what happens to the session objects during  
integration testing; except it looked like they were copied from one  
application call to the next and not reused.

Here is the class I added to test/integration. In my case I was  
interested in setting a timestamp. So there is a separate method for  
that, “set\_time\_value”.

One might like to have a method “set\_session\_value” available in an  
IntegrationTest. This method could just invoke the get  
‘/session\_set/…’  
This would hide this rather clumsy implementation.

## ------- BEGIN FILE test/integration/session\_set\_controller.rb

ActionController::Routing.possible\_controllers \<\< ‘session\_set’

class SessionSetController \< ApplicationController

# 

# Found it impossible to manipulate session values from within an

integration test.

# 

# Add

# 

# require File.dirname( **FILE** ) + ‘/session\_set\_controller’

# 

# Invoke

# 

# get '/session\_set/set\_value, :key=\>‘your\_key’,

:value=\>‘your\_value’

# 

def set\_value  
session[params[:key].to\_sym] = params[:value]  
render :nothing =\> true  
end

def set\_time\_value  
session[params[:key].to\_sym] = Time.parse(params[:value])  
render :nothing =\> true  
end  
end  
------- END FILE test/integration/session\_set\_controller.rb ------------

Related Note: I find the use of the term “session” within integration  
tests a bit unfortunate (open\_session, etc), since there are also these  
session objects, which are however different. Maybe replace by  
“user\_session” ?

Stephan

---

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [August 14, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/setting-session-data-within-integration-test/110779/8 "2007-08-14T17:47:34Z")

</div>

I did see how to get something into the request’s env for integration  
testing:

get ‘/next\_page’, nil, {“HTTP\_REFERER” =\>  
‘[http://localhost:20/last\_page](http://localhost:20/last_page)’}

---

<div class="post-metadata">

**Author:** ![stephanwehner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/0ea827/32.png) [@stephanwehner](https://www.ruby-forum.com/u/stephanwehner)\
**Post date:** [August 23, 2008, 10:20am UTC](https://www.ruby-forum.com/t/setting-session-data-within-integration-test/110779/9 "2008-08-23T10:20:18Z")

</div>

Hey any luck on this issue you have mentioned before? I come across  
the same thing.

On Jul 31 2007, 2:17 am, Stephan W. \<rails-mailing-l…@andreas-
