From ed9567401dfc7b476bf9ccac82826fc63283f708 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Mon, 8 Oct 2012 22:22:47 +0200 Subject: recognizes when a partial was rendered twice. Closes #3675 --- actionpack/CHANGELOG.md | 5 +++++ actionpack/lib/action_controller/test_case.rb | 14 ++++++++++---- actionpack/lib/action_view/test_case.rb | 13 ++++++++----- actionpack/test/fixtures/test/render_two_partials.html.erb | 2 ++ actionpack/test/template/test_case_test.rb | 8 ++++++++ 5 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 actionpack/test/fixtures/test/render_two_partials.html.erb diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index fd02492834..c8773c9179 100644 --- a/actionpack/CHANGELOG.md +++ b/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* diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index ace5a2c822..6aa43edf47 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -123,11 +123,17 @@ module ActionController 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]) + if defined?(@_locals) + actual_locals_collection = @_locals[expected_partial.to_s.sub(/^_/,'')] + result = actual_locals_collection.any? do |actual_locals| + expected_locals.each_pair.all? do |k,v| + v == actual_locals[k] + end end + msg = 'expecting %s to be rendered with %s but was with %s' % [expected_partial, + expected_locals, + actual_locals_collection] + assert(result, msg) else warn "the :locals option to #assert_template is only supported in a ActionView::TestCase" end diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb index 5434b3421e..6e5a3a63ca 100644 --- a/actionpack/lib/action_view/test_case.rb +++ b/actionpack/lib/action_view/test_case.rb @@ -120,7 +120,7 @@ module ActionView end def locals - @locals ||= {} + @_locals ||= {} end included do @@ -162,12 +162,15 @@ module ActionView case options when Hash if block_given? - locals[options[:layout]] = options[:locals] + locals[options[:layout]] ||= [] + locals[options[:layout]] << options[:locals] elsif options.key?(:partial) - locals[options[:partial]] = options[:locals] + locals[options[:partial]] ||= [] + locals[options[:partial]] << options[:locals] end else - locals[options] = local_assigns + locals[options] ||= [] + locals[options] << local_assigns end super @@ -197,7 +200,7 @@ module ActionView :@_routes, :@controller, :@_layouts, - :@locals, + :@_locals, :@method_name, :@output_buffer, :@_partials, diff --git a/actionpack/test/fixtures/test/render_two_partials.html.erb b/actionpack/test/fixtures/test/render_two_partials.html.erb new file mode 100644 index 0000000000..3db6025860 --- /dev/null +++ b/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'} %> diff --git a/actionpack/test/template/test_case_test.rb b/actionpack/test/template/test_case_test.rb index 5265ae6b3a..c7231d9cd5 100644 --- a/actionpack/test/template/test_case_test.rb +++ b/actionpack/test/template/test_case_test.rb @@ -321,6 +321,14 @@ module ActionView 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 -- cgit v1.2.3 From d6524d78687b39c72e0814e61b80e3e6b6d9997e Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 9 Oct 2012 21:14:11 +0200 Subject: 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 --- actionpack/lib/action_controller/test_case.rb | 13 +++------ actionpack/lib/action_view/test_case.rb | 40 +++++++++++++++++++-------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index 6aa43edf47..d911d47a1d 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -123,17 +123,12 @@ module ActionController if expected_partial = options[:partial] if expected_locals = options[:locals] - if defined?(@_locals) - actual_locals_collection = @_locals[expected_partial.to_s.sub(/^_/,'')] - result = actual_locals_collection.any? do |actual_locals| - expected_locals.each_pair.all? do |k,v| - v == actual_locals[k] - end - 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, - actual_locals_collection] - assert(result, msg) + @_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 diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb index 6e5a3a63ca..a548b44780 100644 --- a/actionpack/lib/action_view/test_case.rb +++ b/actionpack/lib/action_view/test_case.rb @@ -119,8 +119,29 @@ module ActionView 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,21 +177,18 @@ module ActionView 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]] ||= [] - locals[options[:layout]] << options[:locals] + rendered_views.add options[:layout], options[:locals] elsif options.key?(:partial) - locals[options[:partial]] ||= [] - locals[options[:partial]] << options[:locals] + rendered_views.add options[:partial], options[:locals] end else - locals[options] ||= [] - locals[options] << local_assigns + rendered_views.add options, local_assigns end super @@ -183,7 +201,7 @@ module ActionView 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 @@ -200,7 +218,7 @@ module ActionView :@_routes, :@controller, :@_layouts, - :@_locals, + :@_rendered_views, :@method_name, :@output_buffer, :@_partials, -- cgit v1.2.3