Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionpack / test / controller / base_test.rb
blob60e61b62855d2d0b5c6af3ead51ae5156ccc07a0
1 require File.dirname(__FILE__) + '/../abstract_unit'
2 require 'test/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.
6 module Submodule
7   class ContainedEmptyController < ActionController::Base
8   end
9   class ContainedNonEmptyController < ActionController::Base
10     def public_action
11     end
12     
13     hide_action :hidden_action
14     def hidden_action
15       raise "Noooo!"
16     end
17     
18     def another_hidden_action
19     end
20     hide_action :another_hidden_action
21   end
22   class SubclassedController < ContainedNonEmptyController
23     hide_action :public_action # Hiding it here should not affect the superclass.
24   end
25 end
26 class EmptyController < ActionController::Base
27 end
28 class NonEmptyController < ActionController::Base
29   def public_action
30   end
31   
32   hide_action :hidden_action
33   def hidden_action
34   end
35 end
37 class MethodMissingController < ActionController::Base
38   
39   hide_action :shouldnt_be_called
40   def shouldnt_be_called
41     raise "NO WAY!"
42   end
43   
44 protected
45   
46   def method_missing(selector)
47     render :text => selector.to_s
48   end
49   
50 end
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
58   end
59   def test_controller_name
60     assert_equal 'empty', EmptyController.controller_name
61     assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
62  end
63 end
65 class ControllerInstanceTests < Test::Unit::TestCase
66   def setup
67     @empty = EmptyController.new
68     @contained = Submodule::ContainedEmptyController.new
69     @empty_controllers = [@empty, @contained, Submodule::SubclassedController.new]
70     
71     @non_empty_controllers = [NonEmptyController.new,
72                               Submodule::ContainedNonEmptyController.new]
73   end
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!"
79     end
80     @non_empty_controllers.each do |c|
81       hide_mocha_methods_from_controller(c)
82       assert_equal Set.new(%w(public_action)), c.send!(:action_methods), "#{c.controller_path} should not be empty!"
83     end
84   end
86   protected
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)
92     end
93 end
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"
108   end
109   
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
115   end
116   
117   def test_method_missing_is_not_an_action_name
118     use_controller MethodMissingController
119     assert ! @controller.send!(:action_methods).include?('method_missing')
120     
121     get :method_missing
122     assert_response :success
123     assert_equal 'method_missing', @response.body
124   end
125   
126   def test_get_on_hidden_should_fail
127     use_controller NonEmptyController
128     get :hidden_action
129     assert_response 404
130     
131     get :another_hidden_action
132     assert_response 404
133   end