1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 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
|
require 'rails_helper'
RSpec.describe "template rendering", type: :controller do
context "without render_views" do
context "with the standard renderers" do
controller do
def index
render template: 'foo', layout: false
end
end
it "renders the 'foo' template" do
get :index
expect(response).to render_template(:foo)
end
it "renders an empty string" do
get :index
expect(response.body).to eq("")
end
end
context "with a String path prepended to the view path" do
controller do
def index
prepend_view_path('app/views/some_templates')
render template: 'bar', layout: false
end
end
it "renders the 'bar' template" do
get :index
expect(response).to render_template(:bar)
end
end
context "with a custom renderer prepended to the view path" do
controller do
def index
prepend_view_path(MyResolver.new)
render template: 'baz', layout: false
end
end
it "renders the 'baz' template" do
get :index
expect(response).to render_template(:baz)
end
it "renders an empty string" do
get :index
expect(response.body).to eq("")
end
end
end
context "with render_views enabled" do
render_views
context "with the standard renderers" do
controller do
def index
render template: 'foo', layout: false
end
end
it "renders the 'foo' template" do
get :index
expect(response).to render_template(:foo)
end
it "renders the contents of the template" do
get :index
expect(response.body).to include("Static template named 'foo.html'")
end
end
context "with a String path prepended to the view path" do
controller do
def index
prepend_view_path('app/views/some_templates')
render template: 'bar', layout: false
end
end
it "renders the 'bar' template" do
get :index
expect(response).to render_template(:bar)
end
it "renders the contents of the template" do
get :index
expect(response.body).to include("Static template named 'bar.html'")
end
end
context "with a custom renderer prepended to the view path" do
controller do
def index
prepend_view_path(MyResolver.new)
render template: 'baz', layout: false
end
end
it "renders the 'baz' template" do
get :index
expect(response).to render_template(:baz)
end
it "renders the contents of the template" do
get :index
expect(response.body).to eq("Dynamic template with path '/baz'")
end
end
end
class MyResolver < ActionView::Resolver
private
def find_templates(name, prefix = nil, partial = false, _details = {}, _key = nil, _locals = [])
name.prepend("_") if partial
path = [prefix, name].join("/")
template = find_template(name, path)
[template]
end
def find_template(name, path)
ActionView::Template.new(
"",
name,
->(template, _source = nil) { %("Dynamic template with path '#{template.virtual_path}'") },
virtual_path: path,
format: :html,
locals: []
)
end
end
end
|