# Rails tests with sessions

**URL:** <https://www.ruby-forum.com/t/rails-tests-with-sessions/120551>\
**Category:** Rails\
**Created:** [November 18, 2007, 12:00pm UTC](https://www.ruby-forum.com/t/rails-tests-with-sessions/120551 "2007-11-18T12:00:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hawe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hawe](https://www.ruby-forum.com/u/hawe)\
**Post date:** [November 18, 2007, 12:00pm UTC](https://www.ruby-forum.com/t/rails-tests-with-sessions/120551/1 "2007-11-18T12:00:38Z")

</div>

Hi

Until recently it was easy to log in a user in a test case by just  
passing a parameter in the get method like this:

get :show, {:id =\> posts(:private\_post).id}, { :user\_id =\>  
users(:tesla).id }

Now with Ruby 1.8.6 and Rails 1.2.5 that doesn’t work anymore. What has  
changed? I’ve tried this:

session[:user\_id] = users(:tesla).id  
get :show, {:id =\> posts(:private\_post).id}

But session[:user\_id] is always nil!

Thanks,  
hawe.

---

<div class="post-metadata">

**Author:** ![hawe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hawe](https://www.ruby-forum.com/u/hawe)\
**Post date:** [November 18, 2007, 1:59pm UTC](https://www.ruby-forum.com/t/rails-tests-with-sessions/120551/2 "2007-11-18T13:59:27Z")

</div>

> I’ve always used @request.session[:foo]=:bar

Yes, I tried that too, but it looses the session information on the way.  
Now I found out that it actually works for the action itself (the  
session value is present), but not for before\_filters (the session value  
went away)!  
And I can still pass the session information in a hash, as before.

So this seems to be a bug?

---

<div class="post-metadata">

**Author:** ![hawe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e7dd6/32.png) [@hawe](https://www.ruby-forum.com/u/hawe)\
**Post date:** [November 18, 2007, 1:30pm UTC](https://www.ruby-forum.com/t/rails-tests-with-sessions/120551/3 "2007-11-18T13:30:42Z")

</div>

On 18 Nov 2007, at 11:00, Heiko Webers wrote:

> has  
> changed? I’ve tried this:
> 
> session[:user\_id] = users(:tesla).id  
> get :show, {:id =\> posts(:private\_post).id}

I’ve always used @request.session[:foo]=:bar

Fred
