1 require 'abstract_unit'
3 class ViewLoadPathsTest
< Test
::Unit::TestCase
4 class TestController
< ActionController
::Base
5 def self.controller_path() "test" end
6 def rescue_action(e
) raise end
8 before_filter
:add_view_path, :only => :hello_world_at_request_time
11 def hello_world_at_request_time() render(:action => 'hello_world') end
15 prepend_view_path
"#{FIXTURE_LOAD_PATH}/override"
19 class Test
::SubController < ActionController
::Base
21 def hello_world
; render(:template => 'test/hello_world'); end
25 TestController
.view_paths
= nil
27 @request = ActionController
::TestRequest.new
28 @response = ActionController
::TestResponse.new
30 @controller = TestController
.new
31 # Following is needed in order to setup @controller.template object properly
32 @controller.send
:initialize_template_class, @response
33 @controller.send
:assign_shortcuts, @request, @response
35 # Track the last warning.
36 @old_behavior = ActiveSupport
::Deprecation.behavior
38 ActiveSupport
::Deprecation.behavior
= Proc
.new
{ |message
, callback
| @last_message = message
}
42 ActiveSupport
::Deprecation.behavior
= @old_behavior
45 def test_template_load_path_was_set_correctly
46 assert_equal
[FIXTURE_LOAD_PATH
], @controller.view_paths
49 def test_controller_appends_view_path_correctly
50 @controller.append_view_path
'foo'
51 assert_equal
[FIXTURE_LOAD_PATH
, 'foo'], @controller.view_paths
53 @controller.append_view_path(%w(bar baz
))
54 assert_equal
[FIXTURE_LOAD_PATH
, 'foo', 'bar', 'baz'], @controller.view_paths
56 @controller.append_view_path(FIXTURE_LOAD_PATH
)
57 assert_equal
[FIXTURE_LOAD_PATH
, 'foo', 'bar', 'baz', FIXTURE_LOAD_PATH
], @controller.view_paths
60 def test_controller_prepends_view_path_correctly
61 @controller.prepend_view_path
'baz'
62 assert_equal
['baz', FIXTURE_LOAD_PATH
], @controller.view_paths
64 @controller.prepend_view_path(%w(foo bar
))
65 assert_equal
['foo', 'bar', 'baz', FIXTURE_LOAD_PATH
], @controller.view_paths
67 @controller.prepend_view_path(FIXTURE_LOAD_PATH
)
68 assert_equal
[FIXTURE_LOAD_PATH
, 'foo', 'bar', 'baz', FIXTURE_LOAD_PATH
], @controller.view_paths
71 def test_template_appends_view_path_correctly
72 @controller.instance_variable_set
:@template, ActionView
::Base.new(TestController
.view_paths
, {}, @controller)
73 class_view_paths
= TestController
.view_paths
75 @controller.append_view_path
'foo'
76 assert_equal
[FIXTURE_LOAD_PATH
, 'foo'], @controller.view_paths
78 @controller.append_view_path(%w(bar baz
))
79 assert_equal
[FIXTURE_LOAD_PATH
, 'foo', 'bar', 'baz'], @controller.view_paths
80 assert_equal class_view_paths
, TestController
.view_paths
83 def test_template_prepends_view_path_correctly
84 @controller.instance_variable_set
:@template, ActionView
::Base.new(TestController
.view_paths
, {}, @controller)
85 class_view_paths
= TestController
.view_paths
87 @controller.prepend_view_path
'baz'
88 assert_equal
['baz', FIXTURE_LOAD_PATH
], @controller.view_paths
90 @controller.prepend_view_path(%w(foo bar
))
91 assert_equal
['foo', 'bar', 'baz', FIXTURE_LOAD_PATH
], @controller.view_paths
92 assert_equal class_view_paths
, TestController
.view_paths
97 assert_response
:success
98 assert_equal
"Hello world!", @response.body
101 def test_view_paths_override
102 TestController
.prepend_view_path
"#{FIXTURE_LOAD_PATH}/override"
104 assert_response
:success
105 assert_equal
"Hello overridden world!", @response.body
108 def test_view_paths_override_for_layouts_in_controllers_with_a_module
109 @controller = Test
::SubController.new
110 Test
::SubController.view_paths
= [ "#{FIXTURE_LOAD_PATH}/override", FIXTURE_LOAD_PATH
, "#{FIXTURE_LOAD_PATH}/override2" ]
112 assert_response
:success
113 assert_equal
"layout: Hello overridden world!", @response.body
116 def test_view_paths_override_at_request_time
117 get
:hello_world_at_request_time
118 assert_response
:success
119 assert_equal
"Hello overridden world!", @response.body
123 original_load_paths
= ActionController
::Base.view_paths
125 self.class.class_eval
%{
126 class A < ActionController::Base; end
128 class C < ActionController::Base; end
131 A.view_paths = ['a/path']
133 assert_equal ['a/path'], A.view_paths
134 assert_equal A.view_paths, B.view_paths
135 assert_equal original_load_paths, C.view_paths
138 assert_nothing_raised { C.view_paths << 'c/path' }
139 assert_equal ['c/path'], C.view_paths