require File.dirname(__FILE__) + '/../abstract_unit' require File.dirname(__FILE__) + '/fake_models' module Fun class GamesController < ActionController::Base def hello_world end end end class TestController < ActionController::Base layout :determine_layout def hello_world end def render_hello_world render :template => "test/hello_world" end def render_hello_world_from_variable @person = "david" render :text => "hello #{@person}" end def render_action_hello_world render :action => "hello_world" end def render_action_hello_world_with_symbol render :action => :hello_world end def render_text_hello_world render :text => "hello world" end def render_json_hello_world render :json => {:hello => 'world'}.to_json end def render_json_hello_world_with_callback render :json => {:hello => 'world'}.to_json, :callback => 'alert' end def render_symbol_json render :json => {:hello => 'world'}.to_json end def render_custom_code render :text => "hello world", :status => 404 end def render_nothing_with_appendix render :text => "appended" end def render_invalid_args render("test/hello") end def render_xml_hello @name = "David" render :template => "test/hello" end def heading head :ok end def greeting # let's just rely on the template end def layout_test render :action => "hello_world" end def builder_layout_test render :action => "hello" end def builder_partial_test render :action => "hello_world_container" end def partials_list @test_unchanged = 'hello' @customers = [ Customer.new("david"), Customer.new("mary") ] render :action => "list" end def partial_only render :partial => true end def hello_in_a_string @customers = [ Customer.new("david"), Customer.new("mary") ] render :text => "How's there? " + render_to_string(:template => "test/list") end def accessing_params_in_template render :inline => "Hello: <%= params[:name] %>" end def accessing_local_assigns_in_inline_template name = params[:local_name] render :inline => "<%= 'Goodbye, ' + local_name %>", :locals => { :local_name => name } end def accessing_local_assigns_in_inline_template_with_string_keys name = params[:local_name] 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 end def formatted_html_erb end def formatted_xml_erb end def render_to_string_test @foo = render_to_string :inline => "this is a test" end def partial render :partial => 'partial' end def partial_dot_html render :partial => 'partial.html.erb' end def partial_as_rjs render :update do |page| page.replace :foo, :partial => 'partial' end end def respond_to_partial_as_rjs respond_to do |format| format.js do render :update do |page| page.replace :foo, :partial => 'partial' end end end end def rescue_action(e) raise end private def determine_layout case action_name when "layout_test"; "layouts/standard" when "builder_layout_test"; "layouts/builder" when "render_symbol_json"; "layouts/standard" # to make sure layouts don't interfere end end end TestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] class RenderTest < Test::Unit::TestCase def setup @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @controller = TestController.new @request.host = "www.nextangle.com" end def test_simple_show get :hello_world assert_response 200 assert_template "test/hello_world" end def test_do_with_render get :render_hello_world assert_template "test/hello_world" end def test_do_with_render_from_variable get :render_hello_world_from_variable assert_equal "hello david", @response.body end def test_do_with_render_action get :render_action_hello_world assert_template "test/hello_world" end def test_do_with_render_action_with_symbol get :render_action_hello_world_with_symbol assert_template "test/hello_world" end def test_do_with_render_text get :render_text_hello_world assert_equal "hello world", @response.body end def test_do_with_render_json get :render_json_hello_world assert_equal '{"hello": "world"}', @response.body assert_equal 'application/json', @response.content_type end def test_do_with_render_json_with_callback get :render_json_hello_world_with_callback assert_equal 'alert({"hello": "world"})', @response.body assert_equal 'application/json', @response.content_type end def test_do_with_render_symbol_json get :render_symbol_json assert_equal '{"hello": "world"}', @response.body assert_equal 'application/json', @response.content_type end def test_do_with_render_custom_code get :render_custom_code assert_response 404 assert_equal 'hello world', @response.body end def test_do_with_render_nothing_with_appendix get :render_nothing_with_appendix assert_response 200 assert_equal 'appended', @response.body end def test_attempt_to_render_with_invalid_arguments assert_raises(ActionController::RenderError) { get :render_invalid_args } end def test_attempt_to_access_object_method assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone } end def test_private_methods assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout } end def test_render_xml get :render_xml_hello assert_equal "\n
Hello David
\nThis is grand!
\n\n", @response.body end def test_render_xml_with_default get :greeting assert_equal "This is grand!
\n", @response.body end def test_render_xml_with_partial get :builder_partial_test assert_equal "Hello
\nThis is grand!
\n\nHello
\nThis is grand!
\n\nHello
\nThis is grand!
\n\n