# Sessions and Integration Tests

**URL:** <https://www.ruby-forum.com/t/sessions-and-integration-tests/78944>\
**Category:** Rails\
**Created:** [December 10, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/sessions-and-integration-tests/78944 "2006-12-10T22:12:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [December 10, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/sessions-and-integration-tests/78944/1 "2006-12-10T22:12:26Z")

</div>

I’ve found that for some complicated controllers, doing things like  
setting sessions in after\_filters, the Integration Tests loose the  
session.

The controllers work fine in development and production. But, under  
Integration Tests, the sessions seem to drop.

I haven’t been able to isolate the bug - that is, build a simple  
controller which demonstrates the behavior.

Mocking out the session by doing something crude like this always fixes  
it:

$session = Hash.new

class ActionController::Base  
def session  
$session  
end  
end

class MyIntegrationTest \< ActionController::IntegrationTest  
…

(Not sure how to unmock that).

Has anyone else experienced this? And, how can I unmock that behavior,  
so that my functional tests aren’t affected?

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [December 10, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/sessions-and-integration-tests/78944/2 "2006-12-10T22:27:00Z")

</div>

Some heavy duty logging/debugging shows that:

- It’s not due to loosing the cookie - the \_session\_id is correct
- The problems happen at the session[…]=… - for some reason, that  
doesn’t call correctly under IntegrationTests

Robert J. wrote:

> I’ve found that for some complicated controllers, doing things like  
> setting sessions in after\_filters, the Integration Tests loose the  
> session.
> 
> The controllers work fine in development and production. But, under  
> Integration Tests, the sessions seem to drop.
> 
> I haven’t been able to isolate the bug - that is, build a simple  
> controller which demonstrates the behavior.
> 
> Mocking out the session by doing something crude like this always fixes  
> it:
> 
> $session = Hash.new
> 
> class ActionController::Base  
> def session  
> $session  
> end  
> end
> 
> class MyIntegrationTest \< ActionController::IntegrationTest  
> …
> 
> (Not sure how to unmock that).
> 
> Has anyone else experienced this? And, how can I unmock that behavior,  
> so that my functional tests aren’t affected?

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [December 11, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/sessions-and-integration-tests/78944/3 "2006-12-11T22:19:54Z")

</div>

What version of Rails, are you using? The Rails 1.2 RC1 has bugs that  
are  
open for some of the Integration testing APIs.

I would take results of the Integration tests with a grain of salt. It  
is  
better to use something like Selenium on Rails as a substitute for  
Integration testing.

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [December 13, 2006, 6:50pm UTC](https://www.ruby-forum.com/t/sessions-and-integration-tests/78944/4 "2006-12-13T18:50:50Z")

</div>

Bala P. wrote:

> What version of Rails, are you using? The Rails 1.2 RC1 has bugs that  
> are  
> open for some of the Integration testing APIs.
> 
> I would take results of the Integration tests with a grain of salt. It  
> is  
> better to use something like Selenium on Rails as a substitute for  
> Integration testing.

1.1.6 - (I think I tried with 1.2rc1 also).

I think the cause is that Integration tests try to capture out the  
controller, invoking some vodoo. It may be better to just leave this  
out - if you need access to the controller, use functional tests.
