render_test.rb 6.9 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 33 34 35 36
  def render_action_hello_world
    render_action "hello_world"
  end
  
  def render_text_hello_world
    render_text "hello world"
  end
D
Initial  
David Heinemeier Hansson 已提交
37

38 39 40 41 42 43 44 45
  def render_custom_code
    render_text "hello world", "404 Moved"
  end
  
  def render_xml_hello
    @name = "David"
    render "test/hello"
  end
46

47 48
  def greeting
    # let's just rely on the template
49 50
  end

51 52 53
  def layout_test
    render_action "hello_world"
  end
54
  
55 56 57 58 59
  def builder_layout_test
    render_action "hello"
  end

  def partials_list
60
    @test_unchanged = 'hello'
61 62 63 64
    @customers = [ Customer.new("david"), Customer.new("mary") ]
    render_action "list"
  end

65 66 67 68
  def partial_only
    render_partial
  end

69 70 71 72
  def hello_in_a_string
    @customers = [ Customer.new("david"), Customer.new("mary") ]
    render_text "How's there? #{render_to_string("test/list")}"
  end
73 74 75 76
  
  def accessing_params_in_template
    render_template "Hello: <%= params[:name] %>"
  end
77

78 79
  def accessing_local_assigns_in_inline_template
    name = params[:local_name]
80 81
    render :inline => "<%= 'Goodbye, ' + local_name %>",
           :locals => { :local_name => name }
82
  end
83 84 85
  
  def accessing_local_assigns_in_inline_template_with_string_keys
    name = params[:local_name]
86 87 88 89
    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
90
  end
91

92 93 94 95
  def render_to_string_test
    @foo = render_to_string :inline => "this is a test"
  end

96
  def rescue_action(e) raise end
97
    
98 99 100 101 102 103
  private
    def determine_layout
      case action_name 
        when "layout_test":         "layouts/standard"
        when "builder_layout_test": "layouts/builder"
      end
D
Initial  
David Heinemeier Hansson 已提交
104
    end
105
end
106

107
TestController.template_root = File.dirname(__FILE__) + "/../fixtures/"
108
Fun::GamesController.template_root = File.dirname(__FILE__) + "/../fixtures/"
109 110

class RenderTest < Test::Unit::TestCase
D
Initial  
David Heinemeier Hansson 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  def setup
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new

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

  def test_simple_show
    @request.action = "hello_world"
    response = process_request
    assert_equal "200 OK", response.headers["Status"]
    assert_equal "test/hello_world", response.template.first_render
  end

  def test_do_with_render
    @request.action = "render_hello_world"
    assert_equal "test/hello_world", process_request.template.first_render
  end

  def test_do_with_render_from_variable
    @request.action = "render_hello_world_from_variable"
    assert_equal "hello david", process_request.body
  end

  def test_do_with_render_action
    @request.action = "render_action_hello_world"
    assert_equal "test/hello_world", process_request.template.first_render
  end

  def test_do_with_render_text
    @request.action = "render_text_hello_world"
    assert_equal "hello world", process_request.body
  end

  def test_do_with_render_custom_code
    @request.action = "render_custom_code"
    assert_equal "404 Moved", process_request.headers["Status"]
  end

  def test_attempt_to_access_object_method
    @request.action = "clone"
    assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { process_request }
  end

155 156 157 158 159
  def test_private_methods
    @request.action = "determine_layout"
    assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { process_request }
  end

D
Initial  
David Heinemeier Hansson 已提交
160
  def test_access_to_request_in_view
161 162
    view_internals_old_value = ActionController::Base.view_controller_internals

D
Initial  
David Heinemeier Hansson 已提交
163
    ActionController::Base.view_controller_internals = false
164
    ActionController::Base.protected_variables_cache = nil
D
Initial  
David Heinemeier Hansson 已提交
165 166 167 168 169 170

    @request.action = "hello_world"
    response = process_request
    assert_nil response.template.assigns["request"]

    ActionController::Base.view_controller_internals = true
171
    ActionController::Base.protected_variables_cache = nil
D
Initial  
David Heinemeier Hansson 已提交
172 173 174

    @request.action = "hello_world"
    response = process_request
175 176 177 178
    assert_kind_of ActionController::AbstractRequest,  response.template.assigns["request"]

    ActionController::Base.view_controller_internals = view_internals_old_value
    ActionController::Base.protected_variables_cache = nil
D
Initial  
David Heinemeier Hansson 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  end
  
  def test_render_xml
    @request.action = "render_xml_hello"
    assert_equal "<html>\n  <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", process_request.body
  end

  def test_render_xml_with_default
    @request.action = "greeting"
    assert_equal "<p>This is grand!</p>\n", process_request.body
  end

  def test_layout_rendering
    @request.action = "layout_test"
    assert_equal "<html>Hello world!</html>", process_request.body
  end

  def test_render_xml_with_layouts
    @request.action = "builder_layout_test"
    assert_equal "<wrapper>\n<html>\n  <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", process_request.body
  end

201 202 203 204
  # def test_partials_list
  #   @request.action = "partials_list"
  #   assert_equal "goodbyeHello: davidHello: marygoodbye\n", process_request.body
  # end
D
Initial  
David Heinemeier Hansson 已提交
205

206 207 208 209 210
  def test_partial_only
    @request.action = "partial_only"
    assert_equal "only partial", process_request.body
  end

211 212
  def test_render_to_string
    @request.action = "hello_in_a_string"
213
    assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", process_request.body
214 215 216 217 218
  end

  def test_render_to_string_resets_assigns
    @request.action = "render_to_string_test"
    assert_equal "The value of foo is: ::this is a test::\n", process_request.body
219 220
  end

221 222 223
  def test_nested_rendering
    @request.action = "hello_world"
    assert_equal "Living in a nested world", Fun::GamesController.process(@request, @response).body
D
Initial  
David Heinemeier Hansson 已提交
224 225
  end

226 227 228 229 230 231
  def test_accessing_params_in_template
    @request.action = "accessing_params_in_template"
    @request.query_parameters[:name] = "David"
    assert_equal "Hello: David", process_request.body
  end

232 233 234 235 236
  def test_accessing_local_assigns_in_inline_template
    @request.action = "accessing_local_assigns_in_inline_template"
    @request.query_parameters[:local_name] = "Local David"
    assert_equal "Goodbye, Local David", process_request.body
  end
237 238 239 240 241 242
  
  def test_accessing_local_assigns_in_inline_template_with_string_keys
    @request.action = "accessing_local_assigns_in_inline_template_with_string_keys"
    @request.query_parameters[:local_name] = "Local David"
    assert_equal "Goodbye, Local David", process_request.body
  end
243

D
Initial  
David Heinemeier Hansson 已提交
244 245 246 247
  private
    def process_request
      TestController.process(@request, @response)
    end
248
end