1 require 'abstract_unit'
3 ActionController
::Base.helpers_dir
= 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_all_helpers_with_alternate_helper_dir
134 @controller_class.helpers_dir
= File
.dirname(__FILE__
) + '/../fixtures/alternate_helpers'
137 @controller_class.master_helper_module
= Module
.new
138 @controller_class.helper
:all
140 # helpers/abc_helper.rb should not be included
141 assert
!master_helper_methods
.include?('bare_a')
143 # alternate_helpers/foo_helper.rb
144 assert master_helper_methods
.include?('baz')
147 def test_helper_proxy
148 methods
= ApplicationController
.helpers
.methods
.map(&:to_s)
151 assert methods
.include?('pluralize')
154 assert methods
.include?('bare_a')
156 # fun/games_helper.rb
157 assert methods
.include?('stratego')
160 assert methods
.include?('foobar')
164 def expected_helper_methods
165 TestHelper
.instance_methods
.map(&:to_s)
168 def master_helper_methods
169 @controller_class.master_helper_module
.instance_methods
.map(&:to_s)
173 expected_helper_methods
- master_helper_methods
176 def test_helper
=(helper_module
)
177 silence_warnings
{ self.class.const_set('TestHelper', helper_module
) }
182 class IsolatedHelpersTest
< Test
::Unit::TestCase
183 class A
< ActionController
::Base
185 render
:inline => '<%= shout %>'
188 def rescue_action(e
) raise end
192 helper
{ def shout
; 'B' end }
195 render
:inline => '<%= shout %>'
200 helper
{ def shout
; 'C' end }
203 render
:inline => '<%= shout %>'
208 @request = ActionController
::TestRequest.new
209 @response = ActionController
::TestResponse.new
210 @request.action
= 'index'
214 assert_raise(NameError
) { A
.process(@request, @response) }
218 assert_equal
'B', B
.process(@request, @response).body
222 assert_equal
'C', C
.process(@request, @response).body