1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/test/controller/new_base/render_action_test.rb

324 lines
9.1 KiB
Ruby
Raw Normal View History

2009-09-19 13:10:41 -04:00
require 'abstract_unit'
2009-03-19 16:35:39 -04:00
module RenderAction
# This has no layout and it works
class BasicController < ActionController::Base
self.view_paths = [ActionView::FixtureResolver.new(
"render_action/basic/hello_world.html.erb" => "Hello world!"
)]
def hello_world
2009-03-19 16:35:39 -04:00
render :action => "hello_world"
end
def hello_world_as_string
2009-03-19 16:35:39 -04:00
render "hello_world"
end
def hello_world_as_string_with_options
2009-03-19 16:35:39 -04:00
render "hello_world", :status => 404
end
def hello_world_as_symbol
2009-03-19 16:35:39 -04:00
render :hello_world
end
def hello_world_with_symbol
2009-03-19 16:35:39 -04:00
render :action => :hello_world
end
def hello_world_with_layout
render :action => "hello_world", :layout => true
end
def hello_world_with_layout_false
render :action => "hello_world", :layout => false
end
def hello_world_with_layout_nil
render :action => "hello_world", :layout => nil
end
def hello_world_with_custom_layout
render :action => "hello_world", :layout => "greetings"
end
2009-03-19 16:35:39 -04:00
end
class RenderActionTest < Rack::TestCase
test "rendering an action using :action => <String>" do
get "/render_action/basic/hello_world"
assert_body "Hello world!"
assert_status 200
end
test "rendering an action using '<action>'" do
get "/render_action/basic/hello_world_as_string"
assert_body "Hello world!"
assert_status 200
end
test "rendering an action using '<action>' and options" do
get "/render_action/basic/hello_world_as_string_with_options"
assert_body "Hello world!"
assert_status 404
end
test "rendering an action using :action" do
get "/render_action/basic/hello_world_as_symbol"
assert_body "Hello world!"
assert_status 200
end
test "rendering an action using :action => :hello_world" do
get "/render_action/basic/hello_world_with_symbol"
assert_body "Hello world!"
assert_status 200
end
2009-03-19 16:35:39 -04:00
end
class RenderLayoutTest < Rack::TestCase
def setup
end
describe "Both <controller_path>.html.erb and application.html.erb are missing"
test "rendering with layout => true" do
assert_raise(ArgumentError) do
get "/render_action/basic/hello_world_with_layout", {}, "action_dispatch.show_exceptions" => false
2009-04-07 20:57:20 -04:00
end
2009-03-19 19:11:20 -04:00
end
test "rendering with layout => false" do
get "/render_action/basic/hello_world_with_layout_false"
assert_body "Hello world!"
assert_status 200
end
test "rendering with layout => :nil" do
get "/render_action/basic/hello_world_with_layout_nil"
assert_body "Hello world!"
assert_status 200
end
test "rendering with layout => 'greetings'" do
assert_raise(ActionView::MissingTemplate) do
get "/render_action/basic/hello_world_with_custom_layout", {}, "action_dispatch.show_exceptions" => false
end
2009-03-19 19:11:20 -04:00
end
end
end
module RenderActionWithApplicationLayout
# # ==== Render actions with layouts ====
2009-04-07 20:57:20 -04:00
class BasicController < ::ApplicationController
# Set the view path to an application view structure with layouts
self.view_paths = [ActionView::FixtureResolver.new(
"render_action_with_application_layout/basic/hello_world.html.erb" => "Hello World!",
"render_action_with_application_layout/basic/hello.html.builder" => "xml.p 'Hello'",
"layouts/application.html.erb" => "Hi <%= yield %> OK, Bye",
"layouts/greetings.html.erb" => "Greetings <%= yield %> Bye",
2009-05-14 18:30:35 -04:00
"layouts/builder.html.builder" => "xml.html do\n xml << yield\nend"
)]
def hello_world
render :action => "hello_world"
end
def hello_world_with_layout
render :action => "hello_world", :layout => true
end
def hello_world_with_layout_false
render :action => "hello_world", :layout => false
end
def hello_world_with_layout_nil
render :action => "hello_world", :layout => nil
end
def hello_world_with_custom_layout
render :action => "hello_world", :layout => "greetings"
end
2009-05-14 18:30:35 -04:00
def with_builder_and_layout
render :action => "hello", :layout => "builder"
end
end
class LayoutTest < Rack::TestCase
describe "Only application.html.erb is present and <controller_path>.html.erb is missing"
test "rendering implicit application.html.erb as layout" do
get "/render_action_with_application_layout/basic/hello_world"
assert_body "Hi Hello World! OK, Bye"
assert_status 200
end
test "rendering with layout => true" do
get "/render_action_with_application_layout/basic/hello_world_with_layout"
assert_body "Hi Hello World! OK, Bye"
assert_status 200
end
test "rendering with layout => false" do
get "/render_action_with_application_layout/basic/hello_world_with_layout_false"
assert_body "Hello World!"
assert_status 200
end
test "rendering with layout => :nil" do
get "/render_action_with_application_layout/basic/hello_world_with_layout_nil"
assert_body "Hello World!"
assert_status 200
end
test "rendering with layout => 'greetings'" do
get "/render_action_with_application_layout/basic/hello_world_with_custom_layout"
assert_body "Greetings Hello World! Bye"
assert_status 200
end
end
class TestLayout < Rack::TestCase
2009-05-14 18:30:35 -04:00
testing BasicController
2009-05-14 18:30:35 -04:00
test "builder works with layouts" do
get :with_builder_and_layout
assert_response "<html>\n<p>Hello</p>\n</html>\n"
2009-05-14 18:30:35 -04:00
end
end
end
module RenderActionWithControllerLayout
class BasicController < ActionController::Base
self.view_paths = [ActionView::FixtureResolver.new(
"render_action_with_controller_layout/basic/hello_world.html.erb" => "Hello World!",
"layouts/render_action_with_controller_layout/basic.html.erb" => "With Controller Layout! <%= yield %> Bye"
)]
def hello_world
render :action => "hello_world"
end
def hello_world_with_layout
render :action => "hello_world", :layout => true
end
def hello_world_with_layout_false
render :action => "hello_world", :layout => false
end
def hello_world_with_layout_nil
render :action => "hello_world", :layout => nil
end
def hello_world_with_custom_layout
render :action => "hello_world", :layout => "greetings"
end
end
class ControllerLayoutTest < Rack::TestCase
describe "Only <controller_path>.html.erb is present and application.html.erb is missing"
test "render hello_world and implicitly use <controller_path>.html.erb as a layout." do
get "/render_action_with_controller_layout/basic/hello_world"
assert_body "With Controller Layout! Hello World! Bye"
assert_status 200
end
test "rendering with layout => true" do
get "/render_action_with_controller_layout/basic/hello_world_with_layout"
assert_body "With Controller Layout! Hello World! Bye"
assert_status 200
end
test "rendering with layout => false" do
get "/render_action_with_controller_layout/basic/hello_world_with_layout_false"
assert_body "Hello World!"
assert_status 200
end
test "rendering with layout => :nil" do
get "/render_action_with_controller_layout/basic/hello_world_with_layout_nil"
assert_body "Hello World!"
assert_status 200
end
end
end
module RenderActionWithBothLayouts
class BasicController < ActionController::Base
self.view_paths = [ActionView::FixtureResolver.new({
"render_action_with_both_layouts/basic/hello_world.html.erb" => "Hello World!",
"layouts/application.html.erb" => "Oh Hi <%= yield %> Bye",
"layouts/render_action_with_both_layouts/basic.html.erb" => "With Controller Layout! <%= yield %> Bye"
})]
def hello_world
render :action => "hello_world"
end
def hello_world_with_layout
render :action => "hello_world", :layout => true
end
def hello_world_with_layout_false
render :action => "hello_world", :layout => false
end
def hello_world_with_layout_nil
render :action => "hello_world", :layout => nil
end
end
class ControllerLayoutTest < Rack::TestCase
describe "Both <controller_path>.html.erb and application.html.erb are present"
test "rendering implicitly use <controller_path>.html.erb over application.html.erb as a layout" do
get "/render_action_with_both_layouts/basic/hello_world"
assert_body "With Controller Layout! Hello World! Bye"
assert_status 200
end
test "rendering with layout => true" do
get "/render_action_with_both_layouts/basic/hello_world_with_layout"
assert_body "With Controller Layout! Hello World! Bye"
assert_status 200
end
test "rendering with layout => false" do
get "/render_action_with_both_layouts/basic/hello_world_with_layout_false"
assert_body "Hello World!"
assert_status 200
end
test "rendering with layout => :nil" do
get "/render_action_with_both_layouts/basic/hello_world_with_layout_nil"
assert_body "Hello World!"
assert_status 200
end
end
end