mirror of
https://github.com/github/rails.git
synced 2026-01-10 15:18:13 -05:00
Backport #5808
df36c5f - Fix assert_template assertion with :layout option 4bd05a7 - Fix assert_template :layout => nil assertion 0d19a08 - Improve assert_template layout checking
This commit is contained in:
@@ -20,7 +20,12 @@ module ActionController
|
||||
|
||||
ActiveSupport::Notifications.subscribe("render_template.action_view") do |name, start, finish, id, payload|
|
||||
path = payload[:layout]
|
||||
@layouts[path] += 1
|
||||
if path
|
||||
@layouts[path] += 1
|
||||
if path =~ /^layouts\/(.*)/
|
||||
@layouts[$1] += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ActiveSupport::Notifications.subscribe("!render_template.action_view") do |name, start, finish, id, payload|
|
||||
@@ -56,6 +61,15 @@ module ActionController
|
||||
# # assert that the "new" view template was rendered
|
||||
# assert_template "new"
|
||||
#
|
||||
# # assert that the layout 'admin' was rendered
|
||||
# assert_template :layout => 'admin'
|
||||
# assert_template :layout => 'layouts/admin'
|
||||
# assert_template :layout => :admin
|
||||
#
|
||||
# # assert that no layout was rendered
|
||||
# assert_template :layout => nil
|
||||
# assert_template :layout => false
|
||||
#
|
||||
# # assert that the "_customer" partial was rendered twice
|
||||
# assert_template :partial => '_customer', :count => 2
|
||||
#
|
||||
@@ -88,17 +102,18 @@ module ActionController
|
||||
end
|
||||
end
|
||||
when Hash
|
||||
if expected_layout = options[:layout]
|
||||
if options.key?(:layout)
|
||||
expected_layout = options[:layout]
|
||||
msg = build_message(message,
|
||||
"expecting layout <?> but action rendered <?>",
|
||||
expected_layout, @layouts.keys)
|
||||
|
||||
case expected_layout
|
||||
when String
|
||||
assert(@layouts.keys.include?(expected_layout), msg)
|
||||
when String, Symbol
|
||||
assert_includes @layouts.keys, expected_layout.to_s, msg
|
||||
when Regexp
|
||||
assert(@layouts.keys.any? {|l| l =~ expected_layout }, msg)
|
||||
when nil
|
||||
when nil, false
|
||||
assert(@layouts.empty?, msg)
|
||||
end
|
||||
end
|
||||
@@ -125,7 +140,7 @@ module ActionController
|
||||
options[:partial], @partials.keys)
|
||||
assert(@partials.include?(expected_partial), msg)
|
||||
end
|
||||
else
|
||||
elsif options.key?(:partial)
|
||||
assert @partials.empty?,
|
||||
"Expected no partials to be rendered"
|
||||
end
|
||||
|
||||
@@ -76,6 +76,11 @@ class ActionPackAssertionsController < ActionController::Base
|
||||
render "test/hello_world", :layout => "layouts/standard"
|
||||
end
|
||||
|
||||
def render_with_layout_and_partial
|
||||
@variable_for_layout = nil
|
||||
render "test/hello_world_with_partial", :layout => "layouts/standard"
|
||||
end
|
||||
|
||||
def session_stuffing
|
||||
session['xmas'] = 'turkey'
|
||||
render :text => "ho ho ho"
|
||||
@@ -483,11 +488,43 @@ class AssertTemplateTest < ActionController::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
def test_fails_expecting_no_layout
|
||||
get :render_with_layout
|
||||
assert_raise(ActiveSupport::TestCase::Assertion) do
|
||||
assert_template :layout => nil
|
||||
end
|
||||
end
|
||||
|
||||
def test_passes_with_correct_layout
|
||||
get :render_with_layout
|
||||
assert_template :layout => "layouts/standard"
|
||||
end
|
||||
|
||||
def test_passes_with_layout_and_partial
|
||||
get :render_with_layout_and_partial
|
||||
assert_template :layout => "layouts/standard"
|
||||
end
|
||||
|
||||
def test_passed_with_no_layout
|
||||
get :hello_world
|
||||
assert_template :layout => nil
|
||||
end
|
||||
|
||||
def test_passed_with_no_layout_false
|
||||
get :hello_world
|
||||
assert_template :layout => false
|
||||
end
|
||||
|
||||
def test_passes_with_correct_layout_without_layouts_prefix
|
||||
get :render_with_layout
|
||||
assert_template :layout => "standard"
|
||||
end
|
||||
|
||||
def test_passes_with_correct_layout_symbol
|
||||
get :render_with_layout
|
||||
assert_template :layout => :standard
|
||||
end
|
||||
|
||||
def test_assert_template_reset_between_requests
|
||||
get :hello_world
|
||||
assert_template 'test/hello_world'
|
||||
|
||||
2
actionpack/test/fixtures/test/hello_world_with_partial.html.erb
vendored
Normal file
2
actionpack/test/fixtures/test/hello_world_with_partial.html.erb
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
Hello world!
|
||||
<%= render '/test/partial' %>
|
||||
Reference in New Issue
Block a user