1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/test/controller/helper_test.rb
Carlhuda c1304098cc Reorganize autoloads:
* A new module (ActiveSupport::Autoload) is provide that extends
    autoloading with new behavior.
  * All autoloads in modules that have extended ActiveSupport::Autoload
    will be eagerly required in threadsafe environments
  * Autoloads can optionally leave off the path if the path is the same
    as full_constant_name.underscore
  * It is possible to specify that a group of autoloads live under an
    additional path. For instance, all of ActionDispatch's middlewares
    are ActionDispatch::MiddlewareName, but they live under 
    "action_dispatch/middlewares/middleware_name"
  * It is possible to specify that a group of autoloads are all found
    at the same path. For instance, a number of exceptions might all
    be declared there.
  * One consequence of this is that testing-related constants are not
    autoloaded. To get the testing helpers for a given component,
    require "component_name/test_case". For instance, "action_controller/test_case".
  * test_help.rb, which is automatically required by a Rails application's
    test helper, requires the test_case.rb for all active components, so
    this change will not be disruptive in existing or new applications.
2009-12-02 20:01:08 -08:00

204 lines
5.1 KiB
Ruby

require 'abstract_unit'
require 'active_support/core_ext/kernel/reporting'
ActionController::Base.helpers_dir = File.dirname(__FILE__) + '/../fixtures/helpers'
module Fun
class GamesController < ActionController::Base
def render_hello_world
render :inline => "hello: <%= stratego %>"
end
def rescue_action(e) raise end
end
class PdfController < ActionController::Base
def test
render :inline => "test: <%= foobar %>"
end
def rescue_action(e) raise end
end
end
class AllHelpersController < ActionController::Base
helper :all
end
module LocalAbcHelper
def a() end
def b() end
def c() end
end
class HelperTest < Test::Unit::TestCase
class TestController < ActionController::Base
attr_accessor :delegate_attr
def delegate_method() end
def rescue_action(e) raise end
end
def setup
# Increment symbol counter.
@symbol = (@@counter ||= 'A0').succ!.dup
# Generate new controller class.
controller_class_name = "Helper#{@symbol}Controller"
eval("class #{controller_class_name} < TestController; end")
@controller_class = self.class.const_get(controller_class_name)
# Set default test helper.
self.test_helper = LocalAbcHelper
end
def test_deprecated_helper
assert_equal expected_helper_methods, missing_methods
assert_nothing_raised { @controller_class.helper TestHelper }
assert_equal [], missing_methods
end
def test_helper_method
assert_nothing_raised { @controller_class.helper_method :delegate_method }
assert master_helper_methods.include?('delegate_method')
end
def test_helper_attr
assert_nothing_raised { @controller_class.helper_attr :delegate_attr }
assert master_helper_methods.include?('delegate_attr')
assert master_helper_methods.include?('delegate_attr=')
end
def call_controller(klass, action)
request = ActionController::TestRequest.new
klass.action(action).call(request.env)
end
def test_helper_for_nested_controller
assert_equal 'hello: Iz guuut!',
call_controller(Fun::GamesController, "render_hello_world").last.body
# request = ActionController::TestRequest.new
#
# resp = Fun::GamesController.action(:render_hello_world).call(request.env)
# assert_equal 'hello: Iz guuut!', resp.last.body
end
def test_helper_for_acronym_controller
assert_equal "test: baz", call_controller(Fun::PdfController, "test").last.body
#
# request = ActionController::TestRequest.new
# response = ActionController::TestResponse.new
# request.action = 'test'
#
# assert_equal 'test: baz', Fun::PdfController.process(request, response).body
end
def test_all_helpers
methods = AllHelpersController._helpers.instance_methods.map {|m| m.to_s}
# abc_helper.rb
assert methods.include?('bare_a')
# fun/games_helper.rb
assert methods.include?('stratego')
# fun/pdf_helper.rb
assert methods.include?('foobar')
end
def test_all_helpers_with_alternate_helper_dir
@controller_class.helpers_dir = File.dirname(__FILE__) + '/../fixtures/alternate_helpers'
# Reload helpers
@controller_class._helpers = Module.new
@controller_class.helper :all
# helpers/abc_helper.rb should not be included
assert !master_helper_methods.include?('bare_a')
# alternate_helpers/foo_helper.rb
assert master_helper_methods.include?('baz')
end
def test_helper_proxy
methods = AllHelpersController.helpers.methods.map(&:to_s)
# ActionView
assert methods.include?('pluralize')
# abc_helper.rb
assert methods.include?('bare_a')
# fun/games_helper.rb
assert methods.include?('stratego')
# fun/pdf_helper.rb
assert methods.include?('foobar')
end
private
def expected_helper_methods
TestHelper.instance_methods.map {|m| m.to_s }
end
def master_helper_methods
@controller_class._helpers.instance_methods.map {|m| m.to_s }
end
def missing_methods
expected_helper_methods - master_helper_methods
end
def test_helper=(helper_module)
silence_warnings { self.class.const_set('TestHelper', helper_module) }
end
end
class IsolatedHelpersTest < Test::Unit::TestCase
class A < ActionController::Base
def index
render :inline => '<%= shout %>'
end
def rescue_action(e) raise end
end
class B < A
helper { def shout; 'B' end }
def index
render :inline => '<%= shout %>'
end
end
class C < A
helper { def shout; 'C' end }
def index
render :inline => '<%= shout %>'
end
end
def call_controller(klass, action)
request = ActionController::TestRequest.new
klass.action(action).call(request.env)
end
def setup
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@request.action = 'index'
end
def test_helper_in_a
assert_raise(ActionView::Template::Error) { call_controller(A, "index") }
end
def test_helper_in_b
assert_equal 'B', call_controller(B, "index").last.body
end
def test_helper_in_c
assert_equal 'C', call_controller(C, "index").last.body
end
end