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

**URL:** <https://www.ruby-forum.com/t/challenge-how-can-i-write-a-test-to-test-the-methods-in-applicationcontroller/128515>\
**Category:** Rails\
**Created:** [February 8, 2008, 11:06am UTC](https://www.ruby-forum.com/t/challenge-how-can-i-write-a-test-to-test-the-methods-in-applicationcontroller/128515 "2008-02-08T11:06:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henry\_Collingridge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/db5fbb/32.png) [@Henry\_Collingridge](https://www.ruby-forum.com/u/Henry_Collingridge)\
**Post date:** [February 8, 2008, 11:06am UTC](https://www.ruby-forum.com/t/challenge-how-can-i-write-a-test-to-test-the-methods-in-applicationcontroller/128515/1 "2008-02-08T11:06:33Z")

</div>

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
