2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2005-09-20 09:55:27 -04:00
|
|
|
require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late
|
2005-02-17 14:00:21 -05:00
|
|
|
|
2006-02-26 12:49:09 -05:00
|
|
|
# Provide some controller to run the tests on.
|
|
|
|
module Submodule
|
|
|
|
class ContainedEmptyController < ActionController::Base
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
2006-02-26 12:49:09 -05:00
|
|
|
class ContainedNonEmptyController < ActionController::Base
|
2005-02-17 14:00:21 -05:00
|
|
|
def public_action
|
|
|
|
end
|
|
|
|
|
2005-02-23 07:52:38 -05:00
|
|
|
hide_action :hidden_action
|
2005-02-17 14:00:21 -05:00
|
|
|
def hidden_action
|
2006-08-13 00:15:22 -04:00
|
|
|
raise "Noooo!"
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
2006-02-26 12:49:09 -05:00
|
|
|
|
|
|
|
def another_hidden_action
|
|
|
|
end
|
|
|
|
hide_action :another_hidden_action
|
|
|
|
end
|
|
|
|
class SubclassedController < ContainedNonEmptyController
|
|
|
|
hide_action :public_action # Hiding it here should not affect the superclass.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class EmptyController < ActionController::Base
|
|
|
|
end
|
|
|
|
class NonEmptyController < ActionController::Base
|
|
|
|
def public_action
|
|
|
|
end
|
|
|
|
|
|
|
|
hide_action :hidden_action
|
|
|
|
def hidden_action
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-08-13 00:15:22 -04:00
|
|
|
class MethodMissingController < ActionController::Base
|
|
|
|
|
|
|
|
hide_action :shouldnt_be_called
|
|
|
|
def shouldnt_be_called
|
|
|
|
raise "NO WAY!"
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def method_missing(selector)
|
|
|
|
render :text => selector.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2005-02-17 14:00:21 -05:00
|
|
|
class ControllerClassTests < Test::Unit::TestCase
|
|
|
|
def test_controller_path
|
2006-02-26 12:49:09 -05:00
|
|
|
assert_equal 'empty', EmptyController.controller_path
|
2006-08-04 21:56:58 -04:00
|
|
|
assert_equal EmptyController.controller_path, EmptyController.new.controller_path
|
2006-02-26 12:49:09 -05:00
|
|
|
assert_equal 'submodule/contained_empty', Submodule::ContainedEmptyController.controller_path
|
2006-08-04 21:56:58 -04:00
|
|
|
assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
|
|
|
def test_controller_name
|
2006-02-26 12:49:09 -05:00
|
|
|
assert_equal 'empty', EmptyController.controller_name
|
|
|
|
assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ControllerInstanceTests < Test::Unit::TestCase
|
|
|
|
def setup
|
2006-02-26 12:49:09 -05:00
|
|
|
@empty = EmptyController.new
|
|
|
|
@contained = Submodule::ContainedEmptyController.new
|
|
|
|
@empty_controllers = [@empty, @contained, Submodule::SubclassedController.new]
|
2005-02-17 14:00:21 -05:00
|
|
|
|
2006-02-26 12:49:09 -05:00
|
|
|
@non_empty_controllers = [NonEmptyController.new,
|
|
|
|
Submodule::ContainedNonEmptyController.new]
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
2005-09-20 03:54:55 -04:00
|
|
|
|
2005-02-17 14:00:21 -05:00
|
|
|
def test_action_methods
|
2005-10-20 17:59:48 -04:00
|
|
|
@empty_controllers.each do |c|
|
2006-09-03 19:34:57 -04:00
|
|
|
hide_mocha_methods_from_controller(c)
|
2007-10-02 01:32:14 -04:00
|
|
|
assert_equal Set.new, c.send!(:action_methods), "#{c.controller_path} should be empty!"
|
2005-10-20 17:59:48 -04:00
|
|
|
end
|
|
|
|
@non_empty_controllers.each do |c|
|
2006-09-03 19:34:57 -04:00
|
|
|
hide_mocha_methods_from_controller(c)
|
2007-10-02 01:32:14 -04:00
|
|
|
assert_equal Set.new(%w(public_action)), c.send!(:action_methods), "#{c.controller_path} should not be empty!"
|
2005-10-20 17:59:48 -04:00
|
|
|
end
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
2007-01-24 07:04:08 -05:00
|
|
|
|
2006-09-03 19:34:57 -04:00
|
|
|
protected
|
2007-01-24 07:04:08 -05:00
|
|
|
# Mocha adds some public instance methods to Object that would be
|
|
|
|
# considered actions, so explicitly hide_action them.
|
|
|
|
def hide_mocha_methods_from_controller(controller)
|
2007-12-29 00:16:03 -05:00
|
|
|
mocha_methods = [
|
|
|
|
:expects, :mocha, :mocha_inspect, :reset_mocha, :stubba_object,
|
|
|
|
:stubba_method, :stubs, :verify, :__metaclass__, :__is_a__, :to_matcher,
|
|
|
|
]
|
2007-10-02 01:32:14 -04:00
|
|
|
controller.class.send!(:hide_action, *mocha_methods)
|
2007-01-24 07:04:08 -05:00
|
|
|
end
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
2006-08-13 00:15:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PerformActionTest < Test::Unit::TestCase
|
|
|
|
def use_controller(controller_class)
|
|
|
|
@controller = controller_class.new
|
|
|
|
|
|
|
|
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
|
|
|
|
# a more accurate simulation of what happens in "real life".
|
|
|
|
@controller.logger = Logger.new(nil)
|
|
|
|
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
|
|
|
|
@request.host = "www.nextangle.com"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_get_on_priv_should_show_selector
|
|
|
|
use_controller MethodMissingController
|
|
|
|
get :shouldnt_be_called
|
|
|
|
assert_response :success
|
|
|
|
assert_equal 'shouldnt_be_called', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_missing_is_not_an_action_name
|
|
|
|
use_controller MethodMissingController
|
2007-10-02 01:32:14 -04:00
|
|
|
assert ! @controller.send!(:action_methods).include?('method_missing')
|
2006-08-13 00:15:22 -04:00
|
|
|
|
|
|
|
get :method_missing
|
|
|
|
assert_response :success
|
|
|
|
assert_equal 'method_missing', @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_get_on_hidden_should_fail
|
|
|
|
use_controller NonEmptyController
|
|
|
|
get :hidden_action
|
|
|
|
assert_response 404
|
|
|
|
|
|
|
|
get :another_hidden_action
|
|
|
|
assert_response 404
|
|
|
|
end
|
2007-01-24 07:04:08 -05:00
|
|
|
end
|