Testing the ApplicationController for assignments

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!

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.

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

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.