From df36c5f7ffd2657e11eea4e407401c9ff2aa0533 Mon Sep 17 00:00:00 2001 From: Alexey Vakhov Date: Wed, 11 Apr 2012 11:28:19 +0600 Subject: Fix assert_template assertion with :layout option --- actionpack/lib/action_controller/test_case.rb | 2 +- actionpack/test/controller/action_pack_assertions_test.rb | 10 ++++++++++ .../test/fixtures/test/hello_world_with_partial.html.erb | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 actionpack/test/fixtures/test/hello_world_with_partial.html.erb diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index 21997c4d79..aee54bf515 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -120,7 +120,7 @@ module ActionController options[:partial], @partials.keys) assert_includes @partials, expected_partial, msg end - else + elsif options.key?(:partial) assert @partials.empty?, "Expected no partials to be rendered" end diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index f5f397c9c0..01151b336b 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -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" @@ -478,6 +483,11 @@ class AssertTemplateTest < ActionController::TestCase 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_assert_template_reset_between_requests get :hello_world assert_template 'test/hello_world' diff --git a/actionpack/test/fixtures/test/hello_world_with_partial.html.erb b/actionpack/test/fixtures/test/hello_world_with_partial.html.erb new file mode 100644 index 0000000000..ec31545356 --- /dev/null +++ b/actionpack/test/fixtures/test/hello_world_with_partial.html.erb @@ -0,0 +1,2 @@ +Hello world! +<%= render '/test/partial' %> -- cgit v1.2.3 From 4bd05a7bdc5003c9ebb090dcb0dc4949e4fb8ad3 Mon Sep 17 00:00:00 2001 From: Alexey Vakhov Date: Wed, 11 Apr 2012 11:55:51 +0600 Subject: Fix assert_template :layout => nil assertion --- actionpack/lib/action_controller/test_case.rb | 7 +++++-- actionpack/test/controller/action_pack_assertions_test.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index aee54bf515..f664da4ffe 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -20,7 +20,9 @@ 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 + end end ActiveSupport::Notifications.subscribe("!render_template.action_view") do |name, start, finish, id, payload| @@ -90,7 +92,8 @@ module ActionController end end when Hash - if expected_layout = options[:layout] + if options.key?(:layout) + expected_layout = options[:layout] msg = message || sprintf("expecting layout <%s> but action rendered <%s>", expected_layout, @layouts.keys) diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index 01151b336b..bc6630a02d 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -478,6 +478,13 @@ 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" @@ -488,6 +495,11 @@ class AssertTemplateTest < ActionController::TestCase assert_template :layout => "layouts/standard" end + def test_passed_with_no_layout + get :hello_world + assert_template :layout => nil + end + def test_assert_template_reset_between_requests get :hello_world assert_template 'test/hello_world' -- cgit v1.2.3 From 0d19a081ee12b46791d8709e5c0898429e2ef91f Mon Sep 17 00:00:00 2001 From: Alexey Vakhov Date: Wed, 11 Apr 2012 12:45:28 +0600 Subject: Improve assert_template layout checking --- actionpack/lib/action_controller/test_case.rb | 18 +++++++++++++++--- .../test/controller/action_pack_assertions_test.rb | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index f664da4ffe..ad02375f12 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -22,6 +22,9 @@ module ActionController path = payload[:layout] if path @layouts[path] += 1 + if path =~ /^layouts\/(.*)/ + @layouts[$1] += 1 + end end end @@ -61,6 +64,15 @@ module ActionController # # assert that the exact template "admin/posts/new" was rendered # assert_template %r{\Aadmin/posts/new\Z} # + # # 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 # @@ -98,11 +110,11 @@ module ActionController expected_layout, @layouts.keys) case expected_layout - when String - assert_includes @layouts.keys, 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 diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index bc6630a02d..9b0d4d0f4c 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -500,6 +500,21 @@ class AssertTemplateTest < ActionController::TestCase 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' -- cgit v1.2.3