Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/actionpack/test/controller/base_test.rb

Revision 8564, 4.1 kB (checked in by bitsweat, 1 year ago)

require abstract_unit directly since test is in load path

Line 
1 require 'abstract_unit'
2 require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late
3
4 # Provide some controller to run the tests on.
5 module Submodule
6   class ContainedEmptyController < ActionController::Base
7   end
8   class ContainedNonEmptyController < ActionController::Base
9     def public_action
10     end
11    
12     hide_action :hidden_action
13     def hidden_action
14       raise "Noooo!"
15     end
16    
17     def another_hidden_action
18     end
19     hide_action :another_hidden_action
20   end
21   class SubclassedController < ContainedNonEmptyController
22     hide_action :public_action # Hiding it here should not affect the superclass.
23   end
24 end
25 class EmptyController < ActionController::Base
26 end
27 class NonEmptyController < ActionController::Base
28   def public_action
29   end
30  
31   hide_action :hidden_action
32   def hidden_action
33   end
34 end
35
36 class MethodMissingController < ActionController::Base
37  
38   hide_action :shouldnt_be_called
39   def shouldnt_be_called
40     raise "NO WAY!"
41   end
42  
43 protected
44  
45   def method_missing(selector)
46     render :text => selector.to_s
47   end
48  
49 end
50
51 class ControllerClassTests < Test::Unit::TestCase
52   def test_controller_path
53     assert_equal 'empty', EmptyController.controller_path
54     assert_equal EmptyController.controller_path, EmptyController.new.controller_path
55     assert_equal 'submodule/contained_empty', Submodule::ContainedEmptyController.controller_path
56     assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path
57   end
58   def test_controller_name
59     assert_equal 'empty', EmptyController.controller_name
60     assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
61  end
62 end
63
64 class ControllerInstanceTests < Test::Unit::TestCase
65   def setup
66     @empty = EmptyController.new
67     @contained = Submodule::ContainedEmptyController.new
68     @empty_controllers = [@empty, @contained, Submodule::SubclassedController.new]
69    
70     @non_empty_controllers = [NonEmptyController.new,
71                               Submodule::ContainedNonEmptyController.new]
72   end
73
74   def test_action_methods
75     @empty_controllers.each do |c|
76       hide_mocha_methods_from_controller(c)
77       assert_equal Set.new, c.send!(:action_methods), "#{c.controller_path} should be empty!"
78     end
79     @non_empty_controllers.each do |c|
80       hide_mocha_methods_from_controller(c)
81       assert_equal Set.new(%w(public_action)), c.send!(:action_methods), "#{c.controller_path} should not be empty!"
82     end
83   end
84
85   protected
86     # Mocha adds some public instance methods to Object that would be
87     # considered actions, so explicitly hide_action them.
88     def hide_mocha_methods_from_controller(controller)
89       mocha_methods = [
90         :expects, :mocha, :mocha_inspect, :reset_mocha, :stubba_object,
91         :stubba_method, :stubs, :verify, :__metaclass__, :__is_a__, :to_matcher,
92       ]
93       controller.class.send!(:hide_action, *mocha_methods)
94     end
95 end
96
97
98 class PerformActionTest < Test::Unit::TestCase
99   def use_controller(controller_class)
100     @controller = controller_class.new
101
102     # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
103     # a more accurate simulation of what happens in "real life".
104     @controller.logger = Logger.new(nil)
105
106     @request    = ActionController::TestRequest.new
107     @response   = ActionController::TestResponse.new
108
109     @request.host = "www.nextangle.com"
110   end
111  
112   def test_get_on_priv_should_show_selector
113     use_controller MethodMissingController
114     get :shouldnt_be_called
115     assert_response :success
116     assert_equal 'shouldnt_be_called', @response.body
117   end
118  
119   def test_method_missing_is_not_an_action_name
120     use_controller MethodMissingController
121     assert ! @controller.send!(:action_methods).include?('method_missing')
122    
123     get :method_missing
124     assert_response :success
125     assert_equal 'method_missing', @response.body
126   end
127  
128   def test_get_on_hidden_should_fail
129     use_controller NonEmptyController
130     get :hidden_action
131     assert_response 404
132    
133     get :another_hidden_action
134     assert_response 404
135   end
136 end
Note: See TracBrowser for help on using the browser.