1 require 'abstract_unit'
2 require 'controller/fake_models'
4 class ViewRenderTest
< Test
::Unit::TestCase
6 @assigns = { :secret => 'in the sauce' }
7 @view = ActionView
::Base.new(ActionController
::Base.view_paths
, @assigns)
12 assert_equal
"Hello world!", @view.render("test/hello_world.erb")
16 def test_render_file_not_using_full_path
17 assert_equal
"Hello world!", @view.render(:file => "test/hello_world.erb")
20 def test_render_file_without_specific_extension
22 assert_equal
"Hello world!", @view.render("test/hello_world")
26 def test_render_file_at_top_level
28 assert_equal
'Elastica', @view.render('/shared')
32 def test_render_file_with_full_path
33 template_path
= File
.join(File
.dirname(__FILE__
), '../fixtures/test/hello_world.erb')
34 assert_equal
"Hello world!", @view.render(:file => template_path
)
37 def test_render_file_with_instance_variables
39 assert_equal
"The secret is in the sauce\n", @view.render("test/render_file_with_ivar.erb")
43 def test_render_file_with_locals
44 locals
= { :secret => 'in the sauce' }
46 assert_equal
"The secret is in the sauce\n", @view.render("test/render_file_with_locals.erb", locals
)
50 def test_render_file_not_using_full_path_with_dot_in_path
52 assert_equal
"The secret is in the sauce\n", @view.render("test/dot.directory/render_file_with_ivar")
56 def test_render_has_access_current_template
58 assert_equal
"test/template.erb", @view.render("test/template.erb")
62 def test_render_update
63 # TODO: You should not have to stub out template because template is self!
64 @view.instance_variable_set(:@template, @view)
65 assert_equal
'alert("Hello, World!");', @view.render(:update) { |page
| page
.alert('Hello, World!') }
68 def test_render_partial
69 assert_equal
"only partial", @view.render(:partial => "test/partial_only")
72 def test_render_partial_with_format
73 assert_equal
'partial html', @view.render(:partial => 'test/partial')
76 def test_render_partial_at_top_level
77 # file fixtures/_top_level_partial_only.erb (not fixtures/test)
78 assert_equal
'top level partial', @view.render(:partial => '/top_level_partial_only')
81 def test_render_partial_with_format_at_top_level
82 # file fixtures/_top_level_partial.html.erb (not fixtures/test, with format extension)
83 assert_equal
'top level partial html', @view.render(:partial => '/top_level_partial')
86 def test_render_partial_with_locals
87 assert_equal
"5", @view.render(:partial => "test/counter", :locals => { :counter_counter => 5 })
90 def test_render_partial_with_errors
91 @view.render(:partial => "test/raise")
92 flunk
"Render did not raise TemplateError"
93 rescue ActionView
::TemplateError => e
94 assert_match
"undefined local variable or method `doesnt_exist'", e
.message
95 assert_equal
"", e
.sub_template_message
96 assert_equal
"1", e
.line_number
97 assert_equal File
.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e
.file_name
100 def test_render_sub_template_with_errors
101 @view.render(:file => "test/sub_template_raise")
102 flunk
"Render did not raise TemplateError"
103 rescue ActionView
::TemplateError => e
104 assert_match
"undefined local variable or method `doesnt_exist'", e
.message
105 assert_equal
"Trace of template inclusion: #{File.expand_path("#{FIXTURE_LOAD_PATH}/test/sub_template_raise.html.erb")}", e.sub_template_message
106 assert_equal
"1", e
.line_number
107 assert_equal File
.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e
.file_name
110 def test_render_partial_collection
111 assert_equal
"Hello: davidHello: mary", @view.render(:partial => "test/customer", :collection => [ Customer
.new("david"), Customer
.new("mary") ])
114 def test_render_partial_collection_as
115 assert_equal
"david david davidmary mary mary",
116 @view.render(:partial => "test/customer_with_var", :collection => [ Customer
.new("david"), Customer
.new("mary") ], :as => :customer)
119 def test_render_partial_collection_without_as
120 assert_equal
"local_inspector,local_inspector_counter,object",
121 @view.render(:partial => "test/local_inspector", :collection => [ Customer
.new("mary") ])
124 def test_render_partial_with_empty_collection_should_return_nil
125 assert_nil
@view.render(:partial => "test/customer", :collection => [])
128 def test_render_partial_with_nil_collection_should_return_nil
129 assert_nil
@view.render(:partial => "test/customer", :collection => nil)
132 def test_render_partial_with_nil_values_in_collection
133 assert_equal
"Hello: davidHello: Anonymous", @view.render(:partial => "test/customer", :collection => [ Customer
.new("david"), nil ])
136 def test_render_partial_with_empty_array_should_return_nil
137 assert_nil
@view.render(:partial => [])
140 # TODO: The reason for this test is unclear, improve documentation
141 def test_render_partial_and_fallback_to_layout
142 assert_equal
"Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" })
145 # TODO: The reason for this test is unclear, improve documentation
146 def test_render_js_partial_and_fallback_to_erb_layout
147 @view.template_format
= :js
148 assert_equal
"Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" })
151 # TODO: The reason for this test is unclear, improve documentation
152 def test_render_missing_xml_partial_and_raise_missing_template
153 @view.template_format
= :xml
154 assert_raise(ActionView
::MissingTemplate) { @view.render(:partial => "test/layout_for_partial") }
157 def test_render_inline
158 assert_equal
"Hello, World!", @view.render(:inline => "Hello, World!")
161 def test_render_inline_with_locals
162 assert_equal
"Hello, Josh!", @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" })
165 def test_render_fallbacks_to_erb_for_unknown_types
166 assert_equal
"Hello, World!", @view.render(:inline => "Hello, World!", :type => :foo)
169 CustomHandler
= lambda
do |template
|
170 "@output_buffer = ''\n" +
171 "@output_buffer << 'source: #{template.source.inspect}'\n"
174 def test_render_inline_with_compilable_custom_type
175 ActionView
::Template.register_template_handler
:foo, CustomHandler
176 assert_equal
'source: "Hello, World!"', @view.render(:inline => "Hello, World!", :type => :foo)
179 def test_render_inline_with_locals_and_compilable_custom_type
180 ActionView
::Template.register_template_handler
:foo, CustomHandler
181 assert_equal
'source: "Hello, <%= name %>!"', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo)
184 def test_render_with_layout
185 assert_equal
%(<title></title>\nHello world!\n),
186 @view.render(:file => "test/hello_world.erb", :layout => "layouts/yield")
189 def test_render_with_nested_layout
190 assert_equal %(<title>title</title>\n<div id="column">column</div>\n<div id="content">content</div>\n),
191 @view.render(:file => "test/nested_layout.erb", :layout => "layouts/yield")