# Testing the ApplicationController for assignments

**URL:** <https://www.ruby-forum.com/t/testing-the-applicationcontroller-for-assignments/68629>\
**Category:** Rails\
**Created:** [September 14, 2006, 12:45am UTC](https://www.ruby-forum.com/t/testing-the-applicationcontroller-for-assignments/68629 "2006-09-14T00:45:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [September 14, 2006, 12:45am UTC](https://www.ruby-forum.com/t/testing-the-applicationcontroller-for-assignments/68629/1 "2006-09-14T00:45:53Z")

</div>

Hi all. I’m using the ApplicationController to set a session variable  
for all other controllers. It works.

However, when performing functional tests, it appears that calling a  
controller doesn’t invoke its parent’s before\_filter method.

I’ve tried, in my tests, calling ApplicationController’s methods  
directly, but I either get weird errors (e.g. “using a symbol as an  
array index session[:cart]”) or the variable assignments (session,  
instance, whatever) don’t propagate to children controllers.

I can test my actual ApplicationController for correctness, but unless I  
tell it to render(:nothing =\> true) (a big no-no if you use your  
imagination) or put in an actual view for it to render I get an error  
during testing saying it couldn’t render anything. I’d rather not have  
random views (okay, 1 random view, but still) floating around just to  
get a test to work.

Has anyone gotten around this? Thanks, much!

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [September 14, 2006, 12:56am UTC](https://www.ruby-forum.com/t/testing-the-applicationcontroller-for-assignments/68629/2 "2006-09-14T00:56:40Z")

</div>

It also seems, at least in the testing environment, that a controller  
outside the one being tested does not have access to the session method.  
A exception is raised stating I’m attempting to issue [] on nil. The  
only occurences of [] are attached to session.

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [September 21, 2006, 10:21pm UTC](https://www.ruby-forum.com/t/testing-the-applicationcontroller-for-assignments/68629/3 "2006-09-21T22:21:20Z")

</div>

Hi guys

I have sort of the same problem:

I get a “Symbol as array index” error when trying to read a session  
value such  
as session[:user\_id]

The only solution I could find by googling around is to append @request  
to  
session: @request.session[:user\_id]

- However this just gives an error saying that the @request object  
wasn’t  
initialised and so the call is interpreted as nil.session

Can anyone help?

Patrick

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [September 30, 2006, 10:33pm UTC](https://www.ruby-forum.com/t/testing-the-applicationcontroller-for-assignments/68629/4 "2006-09-30T22:33:11Z")

</div>

Patrick wrote:

> session: @request.session[:user\_id]
> 
> - However this just gives an error saying that the @request object  
> wasn’t  
> initialised and so the call is interpreted as nil.session

Have you got

` def setup
    @controller = <controller_name>Controller.new
    @request = ActionController::TestRequest.new
    @response = ActionController::TestResponse.new
  end
`  
in your functional test? it should be _generated_ by rails.
