Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

assert_template works when the same partial was rendered multiple times #7886

Merged
merged 2 commits into from

2 participants

@senny
Owner

This is a fix for #3675.

I also refactored the ActionView::TestCase internals, so that a separate object keeps track of rendered views and their locals. The internal instance variable @locals does no longer exist.

@senny
Owner

@steveklabnik @rafaelfranca this fixes #3675 I also did a refactoring because the @locals variable got fairly complex . I intentionally added separate commits so that you an have a look at the version before the refactoring. Let me know if you want to merge it in so that I can squash the commits.

@rafaelfranca

Seems good to me. I would squash the frist three commits.

Please rebase this pull request.

senny added some commits
@senny senny recognizes when a partial was rendered twice. Closes #3675 ed95674
@senny senny refactor `ActionView::TestCase` internals to track rendered locals
this refactoring extracts the semi complex data structure of rendered locals
per view into into a separate class
d6524d7
@senny
Owner

@rafaelfranca squashed and rebased.

@rafaelfranca rafaelfranca merged commit 888a7a5 into rails:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 11, 2012
  1. @senny

    recognizes when a partial was rendered twice. Closes #3675

    senny authored senny committed
  2. @senny

    refactor `ActionView::TestCase` internals to track rendered locals

    senny authored senny committed
    this refactoring extracts the semi complex data structure of rendered locals
    per view into into a separate class
This page is out of date. Refresh to see the latest.
View
5 actionpack/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##
+* `assert_template` can be used to assert on the same template with different locals
+ Fix #3675
+
+ *Yves Senn*
+
* Remove old asset tag concatenation (no longer needed now that we have the asset pipeline). *Josh Peek*
* Accept :remote as symbolic option for `link_to` helper. *Riley Lynch*
View
11 actionpack/lib/action_controller/test_case.rb
@@ -123,11 +123,12 @@ def assert_template(options = {}, message = nil)
if expected_partial = options[:partial]
if expected_locals = options[:locals]
- if defined?(@locals)
- actual_locals = @locals[expected_partial.to_s.sub(/^_/,'')]
- expected_locals.each_pair do |k,v|
- assert_equal(v, actual_locals[k])
- end
+ if defined?(@_rendered_views)
+ view = expected_partial.to_s.sub(/^_/,'')
+ msg = 'expecting %s to be rendered with %s but was with %s' % [expected_partial,
+ expected_locals,
+ @_rendered_views.locals_for(view)]
+ assert(@_rendered_views.view_rendered?(view, options[:locals]), msg)
else
warn "the :locals option to #assert_template is only supported in a ActionView::TestCase"
end
View
37 actionpack/lib/action_view/test_case.rb
@@ -119,8 +119,29 @@ def render(options = {}, local_assigns = {}, &block)
output
end
- def locals
- @locals ||= {}
+ def rendered_views
+ @_rendered_views ||= RenderedViewsCollection.new
+ end
+
+ class RenderedViewsCollection
+ def initialize
+ @rendered_views ||= {}
+ end
+
+ def add(view, locals)
+ @rendered_views[view] ||= []
+ @rendered_views[view] << locals
+ end
+
+ def locals_for(view)
+ @rendered_views[view]
+ end
+
+ def view_rendered?(view, expected_locals)
+ locals_for(view).any? do |actual_locals|
+ expected_locals.all? {|key, value| value == actual_locals[key] }
+ end
+ end
end
included do
@@ -156,18 +177,18 @@ def make_test_case_available_to_view!
end
module Locals
- attr_accessor :locals
+ attr_accessor :rendered_views
def render(options = {}, local_assigns = {})
case options
when Hash
if block_given?
- locals[options[:layout]] = options[:locals]
+ rendered_views.add options[:layout], options[:locals]
elsif options.key?(:partial)
- locals[options[:partial]] = options[:locals]
+ rendered_views.add options[:partial], options[:locals]
end
else
- locals[options] = local_assigns
+ rendered_views.add options, local_assigns
end
super
@@ -180,7 +201,7 @@ def view
view = @controller.view_context
view.singleton_class.send :include, _helpers
view.extend(Locals)
- view.locals = self.locals
+ view.rendered_views = self.rendered_views
view.output_buffer = self.output_buffer
view
end
@@ -197,7 +218,7 @@ def view
:@_routes,
:@controller,
:@_layouts,
- :@locals,
+ :@_rendered_views,
:@method_name,
:@output_buffer,
:@_partials,
View
2  actionpack/test/fixtures/test/render_two_partials.html.erb
@@ -0,0 +1,2 @@
+<%= render :partial => 'partial', :locals => {'first' => '1'} %>
+<%= render :partial => 'partial', :locals => {'second' => '2'} %>
View
8 actionpack/test/template/test_case_test.rb
@@ -321,6 +321,14 @@ class RenderTemplateTest < ActionView::TestCase
assert_template :partial => "_partial_for_use_in_layout", :locals => { :name => "Somebody Else" }
end
end
+
+ test 'supports different locals on the same partial' do
+ controller.controller_path = "test"
+ render(:template => "test/render_two_partials")
+ assert_template partial: '_partial', locals: { 'first' => '1' }
+ assert_template partial: '_partial', locals: { 'second' => '2' }
+ end
+
end
module AHelperWithInitialize
Something went wrong with that request. Please try again.