From c21ab338cb9e38a0bfa51ac8cf4d70285f03c7b2 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Sat, 15 Dec 2012 20:20:34 +0100 Subject: [PATCH 1/2] descriptive `assert_template` error when partial wasn't rendered When `assert_template` is used with the :locals option, and the partial was not rendered, a method_missing error was raised. This changes first checks, if the partial actually was rendered and raises a descriptive error. --- actionpack/lib/action_controller/test_case.rb | 3 +++ actionpack/lib/action_view/test_case.rb | 4 ++++ actionpack/test/template/test_case_test.rb | 9 +++++++++ 3 files changed, 16 insertions(+) diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index 5ae5dd331a03f..9a7f1b5c3a4ea 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -127,6 +127,9 @@ def assert_template(options = {}, message = nil) if expected_locals = options[:locals] if defined?(@_rendered_views) view = expected_partial.to_s.sub(/^_/,'') + partial_was_not_rendered_msg = "expected %s to be rendered but it was not." % view + assert_includes @_rendered_views.rendered_views, view, partial_was_not_rendered_msg + msg = 'expecting %s to be rendered with %s but was with %s' % [expected_partial, expected_locals, @_rendered_views.locals_for(view)] diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb index 4479da5bc4921..1f89e51c66643 100644 --- a/actionpack/lib/action_view/test_case.rb +++ b/actionpack/lib/action_view/test_case.rb @@ -134,6 +134,10 @@ def locals_for(view) @rendered_views[view] end + def rendered_views + @rendered_views.keys + end + def view_rendered?(view, expected_locals) locals_for(view).any? do |actual_locals| expected_locals.all? {|key, value| value == actual_locals[key] } diff --git a/actionpack/test/template/test_case_test.rb b/actionpack/test/template/test_case_test.rb index c7231d9cd5c27..c8441b6894089 100644 --- a/actionpack/test/template/test_case_test.rb +++ b/actionpack/test/template/test_case_test.rb @@ -329,6 +329,15 @@ class RenderTemplateTest < ActionView::TestCase assert_template partial: '_partial', locals: { 'second' => '2' } end + test 'raises descriptive error message when template was not rendered' do + controller.controller_path = "test" + render(template: "test/hello_world_with_partial") + e = assert_raise ActiveSupport::TestCase::Assertion do + assert_template partial: 'i_was_never_rendered', locals: { 'did_not' => 'happen' } + end + assert_match "i_was_never_rendered to be rendered but it was not.", e.message + assert_match 'Expected ["/test/partial"] to include "i_was_never_rendered"', e.message + end end module AHelperWithInitialize From cce94e7232fdd53d5212d85b6f6ecc411baa0b16 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Sat, 15 Dec 2012 20:21:40 +0100 Subject: [PATCH 2/2] partials inside directory work with `assert_template` previously when a partial was placed inside a directory (eg. '/dir/_partial'), `assert_template` did not replace the '_' prefix when looking through rendered tempaltes, which resulted in an error. I modified it to replace both, the leading '_' and the last '_' after a '/'. --- actionpack/CHANGELOG.md | 14 +++++++++++++- actionpack/lib/action_controller/test_case.rb | 3 ++- actionpack/lib/action_view/test_case.rb | 2 +- .../test/_directory/_partial_with_locales.html.erb | 1 + .../test/render_partial_inside_directory.html.erb | 1 + actionpack/test/template/test_case_test.rb | 12 ++++++++++++ 6 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 actionpack/test/fixtures/test/_directory/_partial_with_locales.html.erb create mode 100644 actionpack/test/fixtures/test/render_partial_inside_directory.html.erb diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 69ed117a0355c..fec35a36bd02b 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,17 @@ ## Rails 4.0.0 (unreleased) ## +* `assert_template` can be used to verify the locals of partials, + which live inside a directory. + Fixes #8516. + + # Prefixed partials inside directories worked and still work. + assert_template partial: 'directory/_partial', locals: {name: 'John'} + + # This did not work but does now. + assert_template partial: 'directory/partial', locals: {name: 'John'} + + *Yves Senn* + * Fix `content_tag_for` with array html option. It would embed array as string instead of joining it like `content_tag` does: @@ -248,7 +260,7 @@ * More descriptive error messages when calling `render :partial` with an invalid `:layout` argument. - + Fixes #8376. render partial: 'partial', layout: true diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index 9a7f1b5c3a4ea..bba1f1e201a12 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -126,7 +126,8 @@ def assert_template(options = {}, message = nil) if expected_partial = options[:partial] if expected_locals = options[:locals] if defined?(@_rendered_views) - view = expected_partial.to_s.sub(/^_/,'') + view = expected_partial.to_s.sub(/^_/, '').sub(/\/_(?=[^\/]+\z)/, '/') + partial_was_not_rendered_msg = "expected %s to be rendered but it was not." % view assert_includes @_rendered_views.rendered_views, view, partial_was_not_rendered_msg diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb index 1f89e51c66643..463f192d0cd42 100644 --- a/actionpack/lib/action_view/test_case.rb +++ b/actionpack/lib/action_view/test_case.rb @@ -122,7 +122,7 @@ def rendered_views class RenderedViewsCollection def initialize - @rendered_views ||= {} + @rendered_views ||= Hash.new { |hash, key| hash[key] = [] } end def add(view, locals) diff --git a/actionpack/test/fixtures/test/_directory/_partial_with_locales.html.erb b/actionpack/test/fixtures/test/_directory/_partial_with_locales.html.erb new file mode 100644 index 0000000000000..1cc8d41475fe1 --- /dev/null +++ b/actionpack/test/fixtures/test/_directory/_partial_with_locales.html.erb @@ -0,0 +1 @@ +Hello <%= name %> diff --git a/actionpack/test/fixtures/test/render_partial_inside_directory.html.erb b/actionpack/test/fixtures/test/render_partial_inside_directory.html.erb new file mode 100644 index 0000000000000..1461b95186386 --- /dev/null +++ b/actionpack/test/fixtures/test/render_partial_inside_directory.html.erb @@ -0,0 +1 @@ +<%= render partial: 'test/_directory/partial_with_locales', locals: {'name' => 'Jane'} %> diff --git a/actionpack/test/template/test_case_test.rb b/actionpack/test/template/test_case_test.rb index c8441b6894089..acd002ce73a77 100644 --- a/actionpack/test/template/test_case_test.rb +++ b/actionpack/test/template/test_case_test.rb @@ -338,6 +338,18 @@ class RenderTemplateTest < ActionView::TestCase assert_match "i_was_never_rendered to be rendered but it was not.", e.message assert_match 'Expected ["/test/partial"] to include "i_was_never_rendered"', e.message end + + test 'specifying locals works when the partial is inside a directory with underline prefix' do + controller.controller_path = "test" + render(template: 'test/render_partial_inside_directory') + assert_template partial: 'test/_directory/_partial_with_locales', locals: { 'name' => 'Jane' } + end + + test 'specifying locals works when the partial is inside a directory without underline prefix' do + controller.controller_path = "test" + render(template: 'test/render_partial_inside_directory') + assert_template partial: 'test/_directory/partial_with_locales', locals: { 'name' => 'Jane' } + end end module AHelperWithInitialize