Henry Collingridge
2008-Feb-08 10:06 UTC
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 <snip> 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 <snip> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---