Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

recognizes when a partial was rendered twice. Closes #3675

  • Loading branch information...
commit ed9567401dfc7b476bf9ccac82826fc63283f708 1 parent cd98c25
Yves Senn authored committed
5 actionpack/CHANGELOG.md
Source Rendered
... ... @@ -1,5 +1,10 @@
1 1 ## Rails 4.0.0 (unreleased) ##
2 2
  3 +* `assert_template` can be used to assert on the same template with different locals
  4 + Fix #3675
  5 +
  6 + *Yves Senn*
  7 +
3 8 * Remove old asset tag concatenation (no longer needed now that we have the asset pipeline). *Josh Peek*
4 9
5 10 * Accept :remote as symbolic option for `link_to` helper. *Riley Lynch*
14 actionpack/lib/action_controller/test_case.rb
@@ -123,11 +123,17 @@ def assert_template(options = {}, message = nil)
123 123
124 124 if expected_partial = options[:partial]
125 125 if expected_locals = options[:locals]
126   - if defined?(@locals)
127   - actual_locals = @locals[expected_partial.to_s.sub(/^_/,'')]
128   - expected_locals.each_pair do |k,v|
129   - assert_equal(v, actual_locals[k])
  126 + if defined?(@_locals)
  127 + actual_locals_collection = @_locals[expected_partial.to_s.sub(/^_/,'')]
  128 + result = actual_locals_collection.any? do |actual_locals|
  129 + expected_locals.each_pair.all? do |k,v|
  130 + v == actual_locals[k]
  131 + end
130 132 end
  133 + msg = 'expecting %s to be rendered with %s but was with %s' % [expected_partial,
  134 + expected_locals,
  135 + actual_locals_collection]
  136 + assert(result, msg)
131 137 else
132 138 warn "the :locals option to #assert_template is only supported in a ActionView::TestCase"
133 139 end
13 actionpack/lib/action_view/test_case.rb
@@ -120,7 +120,7 @@ def render(options = {}, local_assigns = {}, &block)
120 120 end
121 121
122 122 def locals
123   - @locals ||= {}
  123 + @_locals ||= {}
124 124 end
125 125
126 126 included do
@@ -162,12 +162,15 @@ def render(options = {}, local_assigns = {})
162 162 case options
163 163 when Hash
164 164 if block_given?
165   - locals[options[:layout]] = options[:locals]
  165 + locals[options[:layout]] ||= []
  166 + locals[options[:layout]] << options[:locals]
166 167 elsif options.key?(:partial)
167   - locals[options[:partial]] = options[:locals]
  168 + locals[options[:partial]] ||= []
  169 + locals[options[:partial]] << options[:locals]
168 170 end
169 171 else
170   - locals[options] = local_assigns
  172 + locals[options] ||= []
  173 + locals[options] << local_assigns
171 174 end
172 175
173 176 super
@@ -197,7 +200,7 @@ def view
197 200 :@_routes,
198 201 :@controller,
199 202 :@_layouts,
200   - :@locals,
  203 + :@_locals,
201 204 :@method_name,
202 205 :@output_buffer,
203 206 :@_partials,
2  actionpack/test/fixtures/test/render_two_partials.html.erb
... ... @@ -0,0 +1,2 @@
  1 +<%= render :partial => 'partial', :locals => {'first' => '1'} %>
  2 +<%= render :partial => 'partial', :locals => {'second' => '2'} %>
8 actionpack/test/template/test_case_test.rb
@@ -321,6 +321,14 @@ class RenderTemplateTest < ActionView::TestCase
321 321 assert_template :partial => "_partial_for_use_in_layout", :locals => { :name => "Somebody Else" }
322 322 end
323 323 end
  324 +
  325 + test 'supports different locals on the same partial' do
  326 + controller.controller_path = "test"
  327 + render(:template => "test/render_two_partials")
  328 + assert_template partial: '_partial', locals: { 'first' => '1' }
  329 + assert_template partial: '_partial', locals: { 'second' => '2' }
  330 + end
  331 +
324 332 end
325 333
326 334 module AHelperWithInitialize

0 comments on commit ed95674

Please sign in to comment.
Something went wrong with that request. Please try again.