From e83e03801aeca6d81ba60d9db653682f83bb13fe Mon Sep 17 00:00:00 2001 From: mark Date: Wed, 10 Dec 2008 22:57:28 -0500 Subject: [PATCH] assert_template learns about partials --- .../assertions/response_assertions.rb | 46 ++++++++++++++++---- actionpack/lib/action_controller/test_process.rb | 4 +- actionpack/lib/action_view/renderable.rb | 5 -- actionpack/lib/action_view/test_case.rb | 21 +++++++++ .../test/controller/action_pack_assertions_test.rb | 6 +- actionpack/test/controller/render_test.rb | 7 +++ 6 files changed, 70 insertions(+), 19 deletions(-) diff --git a/actionpack/lib/action_controller/assertions/response_assertions.rb b/actionpack/lib/action_controller/assertions/response_assertions.rb index 7ab2438..628d134 100644 --- a/actionpack/lib/action_controller/assertions/response_assertions.rb +++ b/actionpack/lib/action_controller/assertions/response_assertions.rb @@ -75,22 +75,50 @@ module ActionController end end - # Asserts that the request was rendered with the appropriate template file. + # Asserts that the request was rendered with the appropriate template file or partials # # ==== Examples # # # assert that the "new" view template was rendered # assert_template "new" # - def assert_template(expected = nil, message=nil) + # # assert that the "_customer" partial was rendered twice + # assert_template :partial => '_customer', :count => 2 + # + # # assert that no partials were rendered + # assert_template :partial => false + # + def assert_template(options = {}, message=nil) clean_backtrace do - rendered = @response.rendered_template.to_s - msg = build_message(message, "expecting but rendering with ", expected, rendered) - assert_block(msg) do - if expected.nil? - @response.rendered_template.blank? - else - rendered.to_s.match(expected) + case options + + when NilClass,String + rendered = @response.rendered[:template].to_s + msg = build_message(message, "expecting but rendering with ", options, rendered) + assert_block(msg) do + if options.nil? + @response.rendered[:template].blank? + else + rendered.to_s.match(options) + end + end + when Hash + if options.has_key?(:partial) + if(options[:partial].is_a?(String)) + if(options[:count]) + # There's almost certainly a better way to do this.. + found = @response.rendered[:partials].select{ |p,_| p.to_s.match(options[:partial]) }.first + real_count = found.nil? ? 0 : found.second + unless real_count == options[:count].to_i + flunk build_message(message, "expecting ? to be rendered ? time(s) but rendered ? time(s)", options[:partial], options[:count].to_i, real_count) + end + else + msg = build_message(message, "expecting partial but action rendered ", options[:partial], @response.rendered[:partials].keys) + assert(@response.rendered[:partials].any?{|p,_| p.to_s.match(options[:partial]) },msg) + end + elsif(options[:partial] == false) + flunk "Expected no partials to be rendered" if !@response.rendered[:partials].empty? + end end end end diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb index cd3914f..78dc382 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/test_process.rb @@ -221,8 +221,8 @@ module ActionController #:nodoc: # Returns the template of the file which was used to # render this response (or nil) - def rendered_template - template.instance_variable_get(:@_first_render) + def rendered + template.instance_variable_get(:@_rendered) end # A shortcut to the flash. Returns an empty hash if no session flash exists. diff --git a/actionpack/lib/action_view/renderable.rb b/actionpack/lib/action_view/renderable.rb index 7258ad0..7c0e62f 100644 --- a/actionpack/lib/action_view/renderable.rb +++ b/actionpack/lib/action_view/renderable.rb @@ -28,11 +28,6 @@ module ActionView stack = view.instance_variable_get(:@_render_stack) stack.push(self) - # This is only used for TestResponse to set rendered_template - unless is_a?(InlineTemplate) || view.instance_variable_get(:@_first_render) - view.instance_variable_set(:@_first_render, self) - end - view.send(:_evaluate_assigns_and_ivars) view.send(:_set_controller_content_type, mime_type) if respond_to?(:mime_type) diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb index 4ab4ed2..0f225e3 100644 --- a/actionpack/lib/action_view/test_case.rb +++ b/actionpack/lib/action_view/test_case.rb @@ -1,4 +1,25 @@ module ActionView + class Base + alias_method :initialize_without_template_tracking, :initialize + def initialize(*args) + @_rendered = {:template => nil, :partials => Hash.new(0)} + initialize_without_template_tracking(*args) + end + end + + module Renderable + alias_method :render_without_template_tracking, :render + def render(view, local_assigns = {}) + if(self.respond_to?(:path) && !is_a?(InlineTemplate)) + rendered = view.instance_variable_get(:@_rendered) + rendered[:partials][self.to_s] += 1 if is_a?(RenderablePartial) + rendered[:template] = self.to_s unless rendered[:template] + view.instance_variable_set(:@_rendered, rendered) + end + render_without_template_tracking(view, local_assigns) + end + end + class TestCase < ActiveSupport::TestCase include ActionController::TestCase::Assertions diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index ea56048..87c12ee 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -326,11 +326,11 @@ class ActionPackAssertionsControllerTest < ActionController::TestCase # check if we were rendered by a file-based template? def test_rendered_action process :nothing - assert_nil @response.rendered_template + assert_nil @response.rendered[:template] process :hello_world - assert @response.rendered_template - assert 'hello_world', @response.rendered_template.to_s + assert @response.rendered[:template] + assert 'hello_world', @response.rendered[:template].to_s end # check the redirection location diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index c5496a9..87733c2 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -1310,21 +1310,28 @@ class RenderTest < ActionController::TestCase def test_partial_collection_with_spacer get :partial_collection_with_spacer assert_equal "Hello: davidonly partialHello: mary", @response.body + assert_template :partial => 'test/_partial_only' + assert_template :partial => '_customer' end def test_partial_collection_shorthand_with_locals get :partial_collection_shorthand_with_locals assert_equal "Bonjour: davidBonjour: mary", @response.body + assert_template :partial => 'customers/_customer', :count => 2 + assert_template :partial => '_completely_fake_and_made_up_template_that_cannot_possibly_be_rendered', :count => 0 end def test_partial_collection_shorthand_with_different_types_of_records get :partial_collection_shorthand_with_different_types_of_records assert_equal "Bonjour bad customer: mark0Bonjour good customer: craig1Bonjour bad customer: john2Bonjour good customer: zach3Bonjour good customer: brandon4Bonjour bad customer: dan5", @response.body + assert_template :partial => 'good_customers/_good_customer', :count => 3 + assert_template :partial => 'bad_customers/_bad_customer', :count => 3 end def test_empty_partial_collection get :empty_partial_collection assert_equal " ", @response.body + assert_template :partial => false end def test_partial_with_hash_object -- 1.5.6.1