# Setting Session Variables in Integration Tests

**URL:** <https://www.ruby-forum.com/t/setting-session-variables-in-integration-tests/167443>\
**Category:** Rails\
**Created:** [May 7, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/setting-session-variables-in-integration-tests/167443 "2009-05-07T20:26:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pinkman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/41988e/32.png) [@pinkman](https://www.ruby-forum.com/u/pinkman)\
**Post date:** [May 7, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/setting-session-variables-in-integration-tests/167443/1 "2009-05-07T20:26:16Z")

</div>

All I want to do is set a session variable when I make a request.  
Here’s my code:

class ProjectIntegrationTest \< ActionController::IntegrationTest  
def test\_truth  
user = user\_for\_test  
user.access\_project\_index\_page  
end

def user\_for\_test  
open\_session do |thing|  
def user.access\_project\_index\_page  
session[:user\_id] = ‘someid’  
get project\_index\_path  
assert\_response :success  
end  
end  
end  
end

I’m getting this error:

test\_truth(ProjectIntegrationTest):  
NoMethodError: undefined method `user’ for  
#ActionController::Integration::Session:0x21d7c50  
method method\_missing in test\_process.rb at line 464  
method send! in integration.rb at line 448  
method method\_missing in integration.rb at line 448  
method user\_for\_test in project\_integration\_test.rb at line 11

Line 11 is the line where “session[:user\_id] = ‘someid’” is being set.  
What am I doing wrong for setting the session?

-Kyle

---

<div class="post-metadata">

**Author:** ![pinkman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/41988e/32.png) [@pinkman](https://www.ruby-forum.com/u/pinkman)\
**Post date:** [May 7, 2009, 9:13pm UTC](https://www.ruby-forum.com/t/setting-session-variables-in-integration-tests/167443/2 "2009-05-07T21:13:11Z")

</div>

On 7 May 2009, at 11:26, Kyle Peyton \<rails-mailing-list@andreas-  
[s.net](http://s.net)\> wrote:

> def user\_for\_test  
> open\_session do |thing|  
> def user.access\_project\_index\_page

Actually it is the above line that is raising the exception - there is  
no user variable / method in scope here.  
This code lois pretty strange - are you really trying to define  
singleton methods on users?

Fred

---

<div class="post-metadata">

**Author:** ![pinkman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/41988e/32.png) [@pinkman](https://www.ruby-forum.com/u/pinkman)\
**Post date:** [May 7, 2009, 9:15pm UTC](https://www.ruby-forum.com/t/setting-session-variables-in-integration-tests/167443/3 "2009-05-07T21:15:32Z")

</div>

I’m pretty new to integration testing and I’m not sure how to define  
tests. I saw an example of someone doing it this way, so that’s the way  
I formed mine. What is the best way to write a normal test that allows  
me to set the “user\_id” session variable so that the “get” can  
successfully happen?

-Kyle

Frederick C. wrote:

> On 7 May 2009, at 11:26, Kyle Peyton \<rails-mailing-list@andreas-  
> [s.net](http://s.net)\> wrote:
> 
> > def user\_for\_test  
> > open\_session do |thing|  
> > def user.access\_project\_index\_page
> 
> Actually it is the above line that is raising the exception - there is  
> no user variable / method in scope here.  
> This code lois pretty strange - are you really trying to define  
> singleton methods on users?
> 
> Fred
