Taro
2008-Jan-12 19:31 UTC
Can''t test application controller methods in functional tests?
I have two methods in application controller that I want to test from
ForumController:
-----------------------
class ApplicationController < ActionController::Base
...
def logout
session[:username] = nil
session[:admin] = nil
redirect_to(request.request_uri)
end
...
def home
redirect_to(:controller => ''forum'', :action =>
''list_forum'')
end
-------------------
class ForumController < ApplicationController
layout "forum"
...
end
-------------------
For method home, action list_forum works according to my test, so that
shouldn''t be the problem. The corresponding tests gives errors:
-------------------
require File.dirname(__FILE__) + ''/../test_helper''
require ''forum_controller''
# Re-raise errors caught by the controller.
class ForumController; def rescue_action(e) raise e end; end
class ForumControllerTest < Test::Unit::TestCase
#add fixtures:
fixtures :messages, :message_threads, :message_boards, :forums
def setup
@controller = ForumController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
...
# test logout (all cases)
def test_logout
get :logout, nil, nil
assert_response :success
assert_nil session[:username]
assert_nil session[:admin]
get :logout, nil, {:username => ''bogus''}
assert_response :success
assert_nil session[:username]
assert_nil session[:admin]
get :logout, nil, {:username => ''Admin'', :admin =>
1}
assert_response :success
assert_nil session[:username]
assert_nil session[:admin]
end
# test home
def test_home
get :home
assert_response :success
assert_redirected_to ''list_forum''
assert_not_nil assigns["forums"]
end
end
-------------------
5) Error:
test_home(ForumControllerTest):
ActionController::RoutingError: No route matches
{:action=>"home", :controller=>"forum"}
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/
action_controller/routing.rb:1299:in `generate''
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/
action_controller/routing.rb:1244:in `generate_extras''
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/
action_controller/routing.rb:1240:in `extra_keys''
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/
action_controller/test_process.rb:105:in `assign_parameters''
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/
action_controller/test_process.rb:380:in `process''
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/
action_controller/test_process.rb:356:in `get''
forum_controller_test.rb:464:in `test_home''
6) Error:
test_logout(ForumControllerTest):
ActionController::RoutingError: No route matches
{:action=>"logout", :controller=>"forum"}
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/
action_controller/routing.rb:1299:in `generate''
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/
action_controller/routing.rb:1244:in `generate_extras''
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/
action_controller/routing.rb:1240:in `extra_keys''
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/
action_controller/test_process.rb:105:in `assign_parameters''
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/
action_controller/test_process.rb:380:in `process''
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/
action_controller/test_process.rb:356:in `get''
forum_controller_test.rb:446:in `test_logout''
-------------------
What''s up with that? Can I not test ApplicationController methods in
ForumController?
Also, how can I set requested uri?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
