render_layout_test.rb 2.9 KB
Newer Older
J
Joshua Peek 已提交
1
require 'abstract_unit'
2 3 4

module ControllerLayouts
  class ImplicitController < ::ApplicationController
5
    self.view_paths = [ActionView::FixtureResolver.new(
6
      "layouts/application.html.erb" => "Main <%= yield %> Layout",
7
      "layouts/override.html.erb"    => "Override! <%= yield %>",
8
      "basic.html.erb"               => "Hello world!",
9
      "controller_layouts/implicit/layout_false.html.erb" => "hi(layout_false.html.erb)"
10
    )]
11

12 13 14
    def index
      render :template => "basic"
    end
15

16 17 18
    def override
      render :template => "basic", :layout => "override"
    end
19

20 21 22
    def layout_false
      render :layout => false
    end
23

24 25
    def builder_override
    end
26
  end
27

28
  class ImplicitNameController < ::ApplicationController
29
    self.view_paths = [ActionView::FixtureResolver.new(
30
      "layouts/controller_layouts/implicit_name.html.erb" => "Implicit <%= yield %> Layout",
31 32
      "basic.html.erb" => "Hello world!"
    )]
33

34 35 36 37
    def index
      render :template => "basic"
    end
  end
38

39
  class RenderLayoutTest < Rack::TestCase
40 41 42
    test "rendering a normal template, but using the implicit layout" do
      get "/controller_layouts/implicit/index"

43
      assert_body   "Main Hello world! Layout"
44 45 46 47 48 49
      assert_status 200
    end

    test "rendering a normal template, but using an implicit NAMED layout" do
      get "/controller_layouts/implicit_name/index"

50
      assert_body "Implicit Hello world! Layout"
51 52 53 54 55 56 57 58
      assert_status 200
    end

    test "overriding an implicit layout with render :layout option" do
      get "/controller_layouts/implicit/override"
      assert_body "Override! Hello world!"
    end

59
  end
60

61
  class LayoutOptionsTest < Rack::TestCase
62
    testing ControllerLayouts::ImplicitController
63

64 65
    test "rendering with :layout => false leaves out the implicit layout" do
      get :layout_false
66
      assert_response "hi(layout_false.html.erb)"
67 68
    end
  end
69 70

  class MismatchFormatController < ::ApplicationController
71
    self.view_paths = [ActionView::FixtureResolver.new(
72
      "layouts/application.html.erb" => "<html><%= yield %></html>",
73 74
      "controller_layouts/mismatch_format/index.js.rjs" => "page[:test].ext",
      "controller_layouts/mismatch_format/implicit.rjs" => "page[:test].ext"      
75 76 77 78 79 80 81
    )]

    def explicit
      render :layout => "application"
    end
  end

82
  class MismatchFormatTest < Rack::TestCase
83 84 85
    testing ControllerLayouts::MismatchFormatController

    test "if JS is selected, an HTML template is not also selected" do
86
      get :index, "format" => "js"
87
      assert_response "$(\"test\").ext();"
88 89 90 91
    end

    test "if JS is implicitly selected, an HTML template is not also selected" do
      get :implicit
92
      assert_response "$(\"test\").ext();"
93 94 95 96 97 98 99 100
    end

    test "if an HTML template is explicitly provides for a JS template, an error is raised" do
      assert_raises ActionView::MissingTemplate do
        get :explicit, {}, "action_dispatch.show_exceptions" => false
      end
    end
  end
101
end