Skip to content

Commit

Permalink
Merge pull request #8520 from senny/8516_assert_template_with_locals
Browse files Browse the repository at this point in the history
Improved `assert_template` when matching :locals with partials
  • Loading branch information
carlosantoniodasilva committed Feb 4, 2013
2 parents e16110c + cce94e7 commit 65113fd
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
14 changes: 13 additions & 1 deletion 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:

Expand Down Expand Up @@ -248,7 +260,7 @@

* More descriptive error messages when calling `render :partial` with
an invalid `:layout` argument.

Fixes #8376.

render partial: 'partial', layout: true
Expand Down
6 changes: 5 additions & 1 deletion actionpack/lib/action_controller/test_case.rb
Expand Up @@ -126,7 +126,11 @@ 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

msg = 'expecting %s to be rendered with %s but was with %s' % [expected_partial,
expected_locals,
@_rendered_views.locals_for(view)]
Expand Down
6 changes: 5 additions & 1 deletion actionpack/lib/action_view/test_case.rb
Expand Up @@ -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)
Expand All @@ -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] }
Expand Down
@@ -0,0 +1 @@
Hello <%= name %>
@@ -0,0 +1 @@
<%= render partial: 'test/_directory/partial_with_locales', locals: {'name' => 'Jane'} %>
21 changes: 21 additions & 0 deletions actionpack/test/template/test_case_test.rb
Expand Up @@ -329,6 +329,27 @@ 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

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
Expand Down

0 comments on commit 65113fd

Please sign in to comment.