1 require File.dirname(__FILE__) + '/../abstract_unit'
3 require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late
5 # Provide some controller to run the tests on.
7 class ContainedEmptyController < ActionController::Base
9 class ContainedNonEmptyController < ActionController::Base
13 hide_action :hidden_action
18 def another_hidden_action
20 hide_action :another_hidden_action
22 class SubclassedController < ContainedNonEmptyController
23 hide_action :public_action # Hiding it here should not affect the superclass.
26 class EmptyController < ActionController::Base
28 class NonEmptyController < ActionController::Base
32 hide_action :hidden_action
37 class MethodMissingController < ActionController::Base
39 hide_action :shouldnt_be_called
40 def shouldnt_be_called
46 def method_missing(selector)
47 render :text => selector.to_s
52 class ControllerClassTests < Test::Unit::TestCase
53 def test_controller_path
54 assert_equal 'empty', EmptyController.controller_path
55 assert_equal EmptyController.controller_path, EmptyController.new.controller_path
56 assert_equal 'submodule/contained_empty', Submodule::ContainedEmptyController.controller_path
57 assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path
59 def test_controller_name
60 assert_equal 'empty', EmptyController.controller_name
61 assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
65 class ControllerInstanceTests < Test::Unit::TestCase
67 @empty = EmptyController.new
68 @contained = Submodule::ContainedEmptyController.new
69 @empty_controllers = [@empty, @contained, Submodule::SubclassedController.new]
71 @non_empty_controllers = [NonEmptyController.new,
72 Submodule::ContainedNonEmptyController.new]
75 def test_action_methods
76 @empty_controllers.each do |c|
77 hide_mocha_methods_from_controller(c)
78 assert_equal Set.new, c.send(:action_methods), "#{c.controller_path} should be empty!"
80 @non_empty_controllers.each do |c|
81 hide_mocha_methods_from_controller(c)
82 assert_equal Set.new('public_action'), c.send(:action_methods), "#{c.controller_path} should not be empty!"
87 # Mocha adds some public instance methods to Object that would be
88 # considered actions, so explicitly hide_action them.
89 def hide_mocha_methods_from_controller(controller)
90 mocha_methods = [:expects, :metaclass, :mocha, :mocha_inspect, :reset_mocha, :stubba_object, :stubba_method, :stubs, :verify, :__metaclass__, :__is_a__]
91 controller.class.send(:hide_action, *mocha_methods)
96 class PerformActionTest < Test::Unit::TestCase
97 def use_controller(controller_class)
98 @controller = controller_class.new
100 # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
101 # a more accurate simulation of what happens in "real life".
102 @controller.logger = Logger.new(nil)
104 @request = ActionController::TestRequest.new
105 @response = ActionController::TestResponse.new
107 @request.host = "www.nextangle.com"
110 def test_get_on_priv_should_show_selector
111 use_controller MethodMissingController
112 get :shouldnt_be_called
113 assert_response :success
114 assert_equal 'shouldnt_be_called', @response.body
117 def test_method_missing_is_not_an_action_name
118 use_controller MethodMissingController
119 assert ! @controller.send(:action_methods).include?('method_missing')
122 assert_response :success
123 assert_equal 'method_missing', @response.body
126 def test_get_on_hidden_should_fail
127 use_controller NonEmptyController
131 get :another_hidden_action