1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionview/test/actionpack/abstract/helper_test.rb

119 lines
3.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "abstract_unit"
2009-03-03 19:42:20 -05:00
ActionController::Base.helpers_path = File.expand_path("../../fixtures/helpers", __dir__)
2009-03-03 19:42:20 -05:00
module AbstractController
module Testing
class ControllerWithHelpers < AbstractController::Base
include AbstractController::Helpers
include AbstractController::Rendering
include ActionView::Rendering
def with_module
2016-08-06 13:36:34 -04:00
render inline: "Module <%= included_method %>"
end
2009-03-03 19:42:20 -05:00
end
2009-03-03 19:42:20 -05:00
module HelperyTest
def included_method
"Included"
end
end
class AbstractHelpers < ControllerWithHelpers
2009-03-03 19:42:20 -05:00
helper(HelperyTest) do
def helpery_test
"World"
end
end
helper :abc
def with_block
2016-08-06 13:36:34 -04:00
render inline: "Hello <%= helpery_test %>"
end
def with_symbol
2016-08-06 13:36:34 -04:00
render inline: "I respond to bare_a: <%= respond_to?(:bare_a) %>"
end
end
class ::HelperyTestController < AbstractHelpers
clear_helpers
end
class AbstractHelpersBlock < ControllerWithHelpers
helper do
include AbstractController::Testing::HelperyTest
2009-03-03 19:42:20 -05:00
end
end
class AbstractInvalidHelpers < AbstractHelpers
include ActionController::Helpers
path = File.expand_path("../../fixtures/helpers_missing", __dir__)
$:.unshift(path)
self.helpers_path = path
end
2009-03-03 19:42:20 -05:00
class TestHelpers < ActiveSupport::TestCase
def setup
@controller = AbstractHelpers.new
2009-03-03 19:42:20 -05:00
end
def test_helpers_with_block
@controller.process(:with_block)
assert_equal "Hello World", @controller.response_body
end
def test_helpers_with_module
@controller.process(:with_module)
assert_equal "Module Included", @controller.response_body
end
def test_helpers_with_symbol
@controller.process(:with_symbol)
assert_equal "I respond to bare_a: true", @controller.response_body
end
def test_declare_missing_helper
e = assert_raise NameError do
AbstractHelpers.helper :missing
end
assert_equal "uninitialized constant MissingHelper", e.message
end
def test_helpers_with_module_through_block
@controller = AbstractHelpersBlock.new
@controller.process(:with_module)
assert_equal "Module Included", @controller.response_body
end
2009-03-03 19:42:20 -05:00
end
class ClearHelpersTest < ActiveSupport::TestCase
def setup
@controller = HelperyTestController.new
end
def test_clears_up_previous_helpers
@controller.process(:with_symbol)
assert_equal "I respond to bare_a: false", @controller.response_body
end
def test_includes_controller_default_helper
@controller.process(:with_block)
assert_equal "Hello Default", @controller.response_body
end
end
class InvalidHelpersTest < ActiveSupport::TestCase
def test_controller_raise_error_about_missing_helper
e = assert_raise(NameError) { AbstractInvalidHelpers.helper(:missing) }
assert_equal "uninitialized constant MissingHelper", e.message
end
end
2009-03-03 19:42:20 -05:00
end
end