Skip to content

Commit

Permalink
Backport #5808
Browse files Browse the repository at this point in the history
df36c5f - Fix assert_template assertion with :layout option
4bd05a7 - Fix assert_template :layout => nil assertion
0d19a08 - Improve assert_template layout checking
  • Loading branch information
MacksMind committed Mar 24, 2013
1 parent 029dd43 commit 74e59ea
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
27 changes: 21 additions & 6 deletions actionpack/lib/action_controller/test_case.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ def setup_subscriptions


ActiveSupport::Notifications.subscribe("render_template.action_view") do |name, start, finish, id, payload| ActiveSupport::Notifications.subscribe("render_template.action_view") do |name, start, finish, id, payload|
path = payload[:layout] path = payload[:layout]
@layouts[path] += 1 if path
@layouts[path] += 1
if path =~ /^layouts\/(.*)/
@layouts[$1] += 1
end
end
end end


ActiveSupport::Notifications.subscribe("!render_template.action_view") do |name, start, finish, id, payload| ActiveSupport::Notifications.subscribe("!render_template.action_view") do |name, start, finish, id, payload|
Expand Down Expand Up @@ -56,6 +61,15 @@ def process(*args)
# # assert that the "new" view template was rendered # # assert that the "new" view template was rendered
# assert_template "new" # 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 that the "_customer" partial was rendered twice
# assert_template :partial => '_customer', :count => 2 # assert_template :partial => '_customer', :count => 2
# #
Expand Down Expand Up @@ -88,17 +102,18 @@ def assert_template(options = {}, message = nil)
end end
end end
when Hash when Hash
if expected_layout = options[:layout] if options.key?(:layout)
expected_layout = options[:layout]
msg = build_message(message, msg = build_message(message,
"expecting layout <?> but action rendered <?>", "expecting layout <?> but action rendered <?>",
expected_layout, @layouts.keys) expected_layout, @layouts.keys)


case expected_layout case expected_layout
when String when String, Symbol
assert(@layouts.keys.include?(expected_layout), msg) assert_includes @layouts.keys, expected_layout.to_s, msg
when Regexp when Regexp
assert(@layouts.keys.any? {|l| l =~ expected_layout }, msg) assert(@layouts.keys.any? {|l| l =~ expected_layout }, msg)
when nil when nil, false
assert(@layouts.empty?, msg) assert(@layouts.empty?, msg)
end end
end end
Expand All @@ -125,7 +140,7 @@ def assert_template(options = {}, message = nil)
options[:partial], @partials.keys) options[:partial], @partials.keys)
assert(@partials.include?(expected_partial), msg) assert(@partials.include?(expected_partial), msg)
end end
else elsif options.key?(:partial)
assert @partials.empty?, assert @partials.empty?,
"Expected no partials to be rendered" "Expected no partials to be rendered"
end end
Expand Down
37 changes: 37 additions & 0 deletions actionpack/test/controller/action_pack_assertions_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ def render_with_layout
render "test/hello_world", :layout => "layouts/standard" render "test/hello_world", :layout => "layouts/standard"
end end


def render_with_layout_and_partial
@variable_for_layout = nil
render "test/hello_world_with_partial", :layout => "layouts/standard"
end

def session_stuffing def session_stuffing
session['xmas'] = 'turkey' session['xmas'] = 'turkey'
render :text => "ho ho ho" render :text => "ho ho ho"
Expand Down Expand Up @@ -483,11 +488,43 @@ def test_fails_with_wrong_layout
end end
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 def test_passes_with_correct_layout
get :render_with_layout get :render_with_layout
assert_template :layout => "layouts/standard" assert_template :layout => "layouts/standard"
end 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 def test_assert_template_reset_between_requests
get :hello_world get :hello_world
assert_template 'test/hello_world' assert_template 'test/hello_world'
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello world!
<%= render '/test/partial' %>

0 comments on commit 74e59ea

Please sign in to comment.