Challenge! How can I write a test to test the methods in ApplicationController?

Hi all,

I’ve been Googling this one for a while now, and haven’t found a
satisfactory answer. It has been posted here before by someone else,
but it seems to have got forgotten, so here goes - perhaps someone can
think up a way around the problem?

In ApplicationController, I have the following method:

class ApplicationController < ActionController::Base

helper_method :admin?

Discover whether the currently logged user is an admin user

def admin?
User.find_by_id(session[:user_id]).admin? unless
session[:user_id].nil?
end

This method works fine in various views, but I can’t seem to test it.
Rails doesn’t generate a test file for ApplicationController, so I
have done that:

require File.dirname(FILE) + ‘/…/test_helper’
require ‘application.rb’

Re-raise errors caught by the controller.

class ApplicationController; def rescue_action(e) raise e end; end

class ApplicationControllerTest < Test::Unit::TestCase
fixtures :users

def logger
RAILS_DEFAULT_LOGGER
end

def setup
@controller = ApplicationController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new

end

def test_admin
# Don’t know how to test “admin?”
assert false
end

end

I can run this test successfully, but if I include a call to admin?, I
get a NoMethodError. I also get the same thing if I try testing it in
another controller (which inherits from application.rb).

Any ideas?

Cheers,

Henry

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs