render_test.rb 6.2 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1 2
require File.dirname(__FILE__) + '/../abstract_unit'

3 4 5
unless defined?(Customer)
  Customer = Struct.new("Customer", :name)
end
D
Initial  
David Heinemeier Hansson 已提交
6

7 8
module Fun
  class GamesController < ActionController::Base
D
Initial  
David Heinemeier Hansson 已提交
9 10
    def hello_world
    end
11 12
  end
end
D
Initial  
David Heinemeier Hansson 已提交
13 14


15 16
class TestController < ActionController::Base
  layout :determine_layout
D
Initial  
David Heinemeier Hansson 已提交
17

18 19
  def hello_world
  end
D
Initial  
David Heinemeier Hansson 已提交
20

21 22 23
  def render_hello_world
    render "test/hello_world"
  end
D
Initial  
David Heinemeier Hansson 已提交
24

25 26 27 28
  def render_hello_world_from_variable
    @person = "david"
    render_text "hello #{@person}"
  end
D
Initial  
David Heinemeier Hansson 已提交
29

30 31 32
  def render_action_hello_world
    render_action "hello_world"
  end
33 34 35 36

  def render_action_hello_world_with_symbol
    render_action :hello_world
  end
J
Jeremy Kemper 已提交
37

38 39 40
  def render_text_hello_world
    render_text "hello world"
  end
D
Initial  
David Heinemeier Hansson 已提交
41

42 43 44
  def render_custom_code
    render_text "hello world", "404 Moved"
  end
J
Jeremy Kemper 已提交
45

46 47 48 49
  def render_xml_hello
    @name = "David"
    render "test/hello"
  end
50

51 52
  def greeting
    # let's just rely on the template
53 54
  end

55 56 57
  def layout_test
    render_action "hello_world"
  end
J
Jeremy Kemper 已提交
58

59 60 61 62 63
  def builder_layout_test
    render_action "hello"
  end

  def partials_list
64
    @test_unchanged = 'hello'
65 66 67 68
    @customers = [ Customer.new("david"), Customer.new("mary") ]
    render_action "list"
  end

69 70 71 72
  def partial_only
    render_partial
  end

73 74 75 76
  def hello_in_a_string
    @customers = [ Customer.new("david"), Customer.new("mary") ]
    render_text "How's there? #{render_to_string("test/list")}"
  end
J
Jeremy Kemper 已提交
77

78 79 80
  def accessing_params_in_template
    render_template "Hello: <%= params[:name] %>"
  end
81

82 83
  def accessing_local_assigns_in_inline_template
    name = params[:local_name]
84 85
    render :inline => "<%= 'Goodbye, ' + local_name %>",
           :locals => { :local_name => name }
86
  end
J
Jeremy Kemper 已提交
87

88 89
  def accessing_local_assigns_in_inline_template_with_string_keys
    name = params[:local_name]
90 91 92 93
    ActionView::Base.local_assigns_support_string_keys = true
    render :inline => "<%= 'Goodbye, ' + local_name %>",
           :locals => { "local_name" => name }
    ActionView::Base.local_assigns_support_string_keys = false
94
  end
95

96 97 98 99
  def render_to_string_test
    @foo = render_to_string :inline => "this is a test"
  end

100
  def rescue_action(e) raise end
J
Jeremy Kemper 已提交
101

102 103
  private
    def determine_layout
J
Jeremy Kemper 已提交
104
      case action_name
105 106 107
        when "layout_test":         "layouts/standard"
        when "builder_layout_test": "layouts/builder"
      end
D
Initial  
David Heinemeier Hansson 已提交
108
    end
109
end
110

111
TestController.template_root = File.dirname(__FILE__) + "/../fixtures/"
112
Fun::GamesController.template_root = File.dirname(__FILE__) + "/../fixtures/"
113 114

class RenderTest < Test::Unit::TestCase
D
Initial  
David Heinemeier Hansson 已提交
115 116 117
  def setup
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
118
    @controller = TestController.new
D
Initial  
David Heinemeier Hansson 已提交
119 120 121 122 123

    @request.host = "www.nextangle.com"
  end

  def test_simple_show
124
    get :hello_world
125 126
    assert_response 200
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
127 128 129
  end

  def test_do_with_render
130
    get :render_hello_world
131
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
132 133 134
  end

  def test_do_with_render_from_variable
135
    get :render_hello_world_from_variable
136
    assert_equal "hello david", @response.body
D
Initial  
David Heinemeier Hansson 已提交
137 138 139
  end

  def test_do_with_render_action
140
    get :render_action_hello_world
141
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
142 143
  end

144
  def test_do_with_render_action_with_symbol
145
    get :render_action_hello_world_with_symbol
146
    assert_template "test/hello_world"
147 148
  end

D
Initial  
David Heinemeier Hansson 已提交
149
  def test_do_with_render_text
150
    get :render_text_hello_world
151
    assert_equal "hello world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
152 153 154
  end

  def test_do_with_render_custom_code
155
    get :render_custom_code
156
    assert_response 404
D
Initial  
David Heinemeier Hansson 已提交
157 158 159
  end

  def test_attempt_to_access_object_method
160
    assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
D
Initial  
David Heinemeier Hansson 已提交
161 162
  end

163
  def test_private_methods
164
    assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
165 166
  end

D
Initial  
David Heinemeier Hansson 已提交
167
  def test_access_to_request_in_view
168 169
    view_internals_old_value = ActionController::Base.view_controller_internals

D
Initial  
David Heinemeier Hansson 已提交
170
    ActionController::Base.view_controller_internals = false
171
    ActionController::Base.protected_variables_cache = nil
D
Initial  
David Heinemeier Hansson 已提交
172

173 174
    get :hello_world
    assert_nil assigns["request"]
D
Initial  
David Heinemeier Hansson 已提交
175 176

    ActionController::Base.view_controller_internals = true
177
    ActionController::Base.protected_variables_cache = nil
D
Initial  
David Heinemeier Hansson 已提交
178

179 180
    get :hello_world
    assert_kind_of ActionController::AbstractRequest,  assigns["request"]
181 182 183

    ActionController::Base.view_controller_internals = view_internals_old_value
    ActionController::Base.protected_variables_cache = nil
D
Initial  
David Heinemeier Hansson 已提交
184
  end
J
Jeremy Kemper 已提交
185

D
Initial  
David Heinemeier Hansson 已提交
186
  def test_render_xml
187
    get :render_xml_hello
188
    assert_equal "<html>\n  <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
189 190 191
  end

  def test_render_xml_with_default
192
    get :greeting
193
    assert_equal "<p>This is grand!</p>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
194 195 196
  end

  def test_layout_rendering
197
    get :layout_test
198
    assert_equal "<html>Hello world!</html>", @response.body
D
Initial  
David Heinemeier Hansson 已提交
199 200 201
  end

  def test_render_xml_with_layouts
202
    get :builder_layout_test
203
    assert_equal "<wrapper>\n<html>\n  <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
204 205
  end

206
  # def test_partials_list
207
  #   get :partials_list
208 209
  #   assert_equal "goodbyeHello: davidHello: marygoodbye\n", process_request.body
  # end
D
Initial  
David Heinemeier Hansson 已提交
210

211
  def test_partial_only
212
    get :partial_only
213
    assert_equal "only partial", @response.body
214 215
  end

216
  def test_render_to_string
217
    get :hello_in_a_string
218
    assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
219 220 221
  end

  def test_render_to_string_resets_assigns
222
    get :render_to_string_test
223
    assert_equal "The value of foo is: ::this is a test::\n", @response.body
224 225
  end

226
  def test_nested_rendering
227 228 229
    @controller = Fun::GamesController.new
    get :hello_world
    assert_equal "Living in a nested world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
230 231
  end

232
  def test_accessing_params_in_template
233
    get :accessing_params_in_template, :name => "David"
234
    assert_equal "Hello: David", @response.body
235 236
  end

237
  def test_accessing_local_assigns_in_inline_template
238
    get :accessing_local_assigns_in_inline_template, :local_name => "Local David"
239
    assert_equal "Goodbye, Local David", @response.body
240
  end
J
Jeremy Kemper 已提交
241

242
  def test_accessing_local_assigns_in_inline_template_with_string_keys
243
    get :accessing_local_assigns_in_inline_template_with_string_keys, :local_name => "Local David"
244
    assert_equal "Goodbye, Local David", @response.body
245
  end
246
end