# Accessing session attributes from an integration test

**URL:** <https://www.ruby-forum.com/t/accessing-session-attributes-from-an-integration-test/88360>\
**Category:** Rails\
**Created:** [March 10, 2007, 1:03am UTC](https://www.ruby-forum.com/t/accessing-session-attributes-from-an-integration-test/88360 "2007-03-10T01:03:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nielsene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@nielsene](https://www.ruby-forum.com/u/nielsene)\
**Post date:** [March 10, 2007, 1:03am UTC](https://www.ruby-forum.com/t/accessing-session-attributes-from-an-integration-test/88360/1 "2007-03-10T01:03:34Z")

</div>

I have a very simple integration test based off of an example and  
Agile Web D. with Rails but I can’t seem to get it to work.  
I’m simply setting a user id in the session via the get method in the  
test but then can’t access it when it hits my login controller.

Here is the relevant code in the integration test

* * *

## get :index, {}, { :user\_id =\> users(:bart).id } assert\_response :success

## and in the login controller that gets called by the filter

def authorize  
puts “User ID from session[#{session[:user\_id]}]”

```
unless session[:user_id]
  flash[:notice] = "Please log in"
  redirect_to(:controller => 'login', :action => 'login')
end

```

## end

## which prints out

## #\<ActionController::TestSession:0x38a08c4 @attributes={:user\_id=\>1, “flash”=\>{}}, @saved\_attributes=nil, @session\_id=""\> User ID from session[]

and then fails because the redirect is carried out. Anybody know why  
the attribute is set but not accessible via session[:user\_id]? The  
code works fine when I manually try it out deployed.

---

<div class="post-metadata">

**Author:** ![nielsene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@nielsene](https://www.ruby-forum.com/u/nielsene)\
**Post date:** [March 10, 2007, 2:50am UTC](https://www.ruby-forum.com/t/accessing-session-attributes-from-an-integration-test/88360/2 "2007-03-10T02:50:42Z")

</div>

Make sure that you are using the right key for the user id. Run rake  
db:sessions:clear, restart your server and try again.

---

<div class="post-metadata">

**Author:** ![nielsene](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bc79bd/32.png) [@nielsene](https://www.ruby-forum.com/u/nielsene)\
**Post date:** [March 10, 2007, 12:58pm UTC](https://www.ruby-forum.com/t/accessing-session-attributes-from-an-integration-test/88360/3 "2007-03-10T12:58:32Z")

</div>

I had to change

get :index, {}, { :user\_id =\> users(:bart).id }

to

get :index, {}, { ‘user\_id’ =\> users(:bart).id }

The book example uses the original symbol version.
