1 require 'abstract_unit'
3 ActionController
::Helpers::HELPERS_DIR.replace File
.dirname(__FILE__
) + '/../fixtures/helpers'
5 class TestController
< ActionController
::Base
6 attr_accessor
:delegate_attr
7 def delegate_method() end
8 def rescue_action(e
) raise end
12 class GamesController
< ActionController
::Base
13 def render_hello_world
14 render
:inline => "hello: <%= stratego %>"
17 def rescue_action(e
) raise end
20 class PdfController
< ActionController
::Base
22 render
:inline => "test: <%= foobar %>"
25 def rescue_action(e
) raise end
29 class ApplicationController
< ActionController
::Base
39 class HelperTest
< Test
::Unit::TestCase
41 # Increment symbol counter.
42 @symbol = (@
@counter ||= 'A0').succ
!.dup
44 # Generate new controller class.
45 controller_class_name
= "Helper#{@symbol}Controller"
46 eval("class #{controller_class_name} < TestController; end")
47 @controller_class = self.class.const_get(controller_class_name
)
49 # Set default test helper.
50 self.test_helper
= LocalAbcHelper
53 def test_deprecated_helper
54 assert_equal expected_helper_methods
, missing_methods
55 assert_nothing_raised
{ @controller_class.helper TestHelper
}
56 assert_equal
[], missing_methods
59 def test_declare_helper
61 self.test_helper
= AbcHelper
62 assert_equal expected_helper_methods
, missing_methods
63 assert_nothing_raised
{ @controller_class.helper
:abc }
64 assert_equal
[], missing_methods
67 def test_declare_missing_helper
68 assert_equal expected_helper_methods
, missing_methods
69 assert_raise(MissingSourceFile
) { @controller_class.helper
:missing }
72 def test_declare_missing_file_from_helper
73 require 'broken_helper'
75 assert_nil(/\bbroken_helper\b/.match(e
.to_s
)[1])
79 assert_nothing_raised
{
80 @controller_class.helper
{ def block_helper_method
; end }
82 assert master_helper_methods
.include?('block_helper_method')
85 def test_helper_block_include
86 assert_equal expected_helper_methods
, missing_methods
87 assert_nothing_raised
{
88 @controller_class.helper
{ include HelperTest
::TestHelper }
90 assert
[], missing_methods
93 def test_helper_method
94 assert_nothing_raised
{ @controller_class.helper_method
:delegate_method }
95 assert master_helper_methods
.include?('delegate_method')
99 assert_nothing_raised
{ @controller_class.helper_attr
:delegate_attr }
100 assert master_helper_methods
.include?('delegate_attr')
101 assert master_helper_methods
.include?('delegate_attr=')
104 def test_helper_for_nested_controller
105 request
= ActionController
::TestRequest.new
106 response
= ActionController
::TestResponse.new
107 request
.action
= 'render_hello_world'
109 assert_equal
'hello: Iz guuut!', Fun
::GamesController.process(request
, response
).body
112 def test_helper_for_acronym_controller
113 request
= ActionController
::TestRequest.new
114 response
= ActionController
::TestResponse.new
115 request
.action
= 'test'
117 assert_equal
'test: baz', Fun
::PdfController.process(request
, response
).body
121 methods
= ApplicationController
.master_helper_module
.instance_methods
.map(&:to_s)
124 assert methods
.include?('bare_a')
126 # fun/games_helper.rb
127 assert methods
.include?('stratego')
130 assert methods
.include?('foobar')
133 def test_helper_proxy
134 methods
= ApplicationController
.helpers
.methods
.map(&:to_s)
137 assert methods
.include?('pluralize')
140 assert methods
.include?('bare_a')
142 # fun/games_helper.rb
143 assert methods
.include?('stratego')
146 assert methods
.include?('foobar')
150 def expected_helper_methods
151 TestHelper
.instance_methods
.map(&:to_s)
154 def master_helper_methods
155 @controller_class.master_helper_module
.instance_methods
.map(&:to_s)
159 expected_helper_methods
- master_helper_methods
162 def test_helper
=(helper_module
)
163 silence_warnings
{ self.class.const_set('TestHelper', helper_module
) }
168 class IsolatedHelpersTest
< Test
::Unit::TestCase
169 class A
< ActionController
::Base
171 render
:inline => '<%= shout %>'
174 def rescue_action(e
) raise end
178 helper
{ def shout
; 'B' end }
181 render
:inline => '<%= shout %>'
186 helper
{ def shout
; 'C' end }
189 render
:inline => '<%= shout %>'
194 @request = ActionController
::TestRequest.new
195 @response = ActionController
::TestResponse.new
196 @request.action
= 'index'
200 assert_raise(NameError
) { A
.process(@request, @response) }
204 assert_equal
'B', B
.process(@request, @response).body
208 assert_equal
'C', C
.process(@request, @response).body