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
4 # Provide some controller to run the tests on.
6 class ContainedEmptyController
< ActionController
::Base
8 class ContainedNonEmptyController
< ActionController
::Base
10 render
:nothing => true
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 DefaultUrlOptionsController < ActionController::Base
53 def default_url_options_action
56 def default_url_options(options = nil)
57 { :host => 'www.override.com', :action => 'new', :bacon => 'chunky' }
61 class ControllerClassTests < Test::Unit::TestCase
62 def test_controller_path
63 assert_equal 'empty', EmptyController.controller_path
64 assert_equal EmptyController.controller_path, EmptyController.new.controller_path
65 assert_equal 'submodule/contained_empty', Submodule::ContainedEmptyController.controller_path
66 assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path
68 def test_controller_name
69 assert_equal 'empty', EmptyController.controller_name
70 assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
74 class ControllerInstanceTests < Test::Unit::TestCase
76 @empty = EmptyController.new
77 @contained = Submodule::ContainedEmptyController.new
78 @empty_controllers = [@empty, @contained, Submodule::SubclassedController.new]
80 @non_empty_controllers = [NonEmptyController.new,
81 Submodule::ContainedNonEmptyController.new]
84 def test_action_methods
85 @empty_controllers.each do |c|
86 hide_mocha_methods_from_controller(c)
87 assert_equal Set.new, c.__send__(:action_methods), "#{c.controller_path} should be empty!
"
89 @non_empty_controllers.each do |c|
90 hide_mocha_methods_from_controller(c)
91 assert_equal Set.new(%w(public_action)), c.__send__(:action_methods), "#{c.controller_path} should
not be empty!
"
96 # Mocha adds some public instance methods to Object that would be
97 # considered actions, so explicitly hide_action them.
98 def hide_mocha_methods_from_controller(controller)
100 :expects, :mocha, :mocha_inspect, :reset_mocha, :stubba_object,
101 :stubba_method, :stubs, :verify, :__metaclass__, :__is_a__, :to_matcher,
103 controller.class.__send__(:hide_action, *mocha_methods)
108 class PerformActionTest < ActionController::TestCase
116 def method_missing(method, *args)
117 @logged << args.first
121 def use_controller(controller_class)
122 @controller = controller_class.new
124 # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
125 # a more accurate simulation of what happens in "real life
".
126 @controller.logger = Logger.new(nil)
128 @request = ActionController::TestRequest.new
129 @response = ActionController::TestResponse.new
131 @request.host = "www
.nextangle
.com
"
133 rescue_action_in_public!
136 def test_get_on_priv_should_show_selector
137 use_controller MethodMissingController
138 get
:shouldnt_be_called
139 assert_response
:success
140 assert_equal
'shouldnt_be_called', @response.body
143 def test_method_missing_is_not_an_action_name
144 use_controller MethodMissingController
145 assert !
@controller.__send__(:action_methods).include?('method_missing')
148 assert_response
:success
149 assert_equal
'method_missing', @response.body
152 def test_get_on_hidden_should_fail
153 use_controller NonEmptyController
157 get
:another_hidden_action
161 def test_namespaced_action_should_log_module_name
162 use_controller Submodule
::ContainedNonEmptyController
163 @controller.logger
= MockLogger
.new
165 assert_match
/Processing\sSubmodule::ContainedNonEmptyController#public_action/, @controller.logger
.logged
[1]
169 class DefaultUrlOptionsTest
< ActionController
::TestCase
170 tests DefaultUrlOptionsController
173 @request.host
= 'www.example.com'
174 rescue_action_in_public!
177 def test_default_url_options_are_used_if_set
178 ActionController
::Routing::Routes.draw
do |map
|
179 map
.default_url_options
'default_url_options', :controller => 'default_url_options'
180 map
.connect
':controller/:action/:id'
183 get
:default_url_options_action # Make a dummy request so that the controller is initialized properly.
185 assert_equal
'http://www.override.com/default_url_options/new?bacon=chunky', @controller.url_for(:controller => 'default_url_options')
186 assert_equal
'http://www.override.com/default_url_options?bacon=chunky', @controller.send(:default_url_options_url)
188 ActionController
::Routing::Routes.load!
192 class EmptyUrlOptionsTest
< ActionController
::TestCase
193 tests NonEmptyController
196 @request.host
= 'www.example.com'
197 rescue_action_in_public!
200 def test_ensure_url_for_works_as_expected_when_called_with_no_options_if_default_url_options_is_not_set
202 assert_equal
"http://www.example.com/non_empty/public_action", @controller.url_for
206 class EnsureNamedRoutesWorksTicket22BugTest
< Test
::Unit::TestCase
207 def test_named_routes_still_work
208 ActionController
::Routing::Routes.draw
do |map
|
209 map
.resources
:things
211 EmptyController
.send
:include, ActionController
::UrlWriter
213 assert_equal
'/things', EmptyController
.new
.send(:things_path)
215 ActionController
::Routing::Routes.load!