From 947f86c699b33bd44703b3554db58e4cfca37c86 Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Wed, 17 Mar 2010 14:19:42 -0700 Subject: Modify assert_template to use instrumentation --- actionpack/lib/action_controller/test_case.rb | 82 ++++++++++++++++++++++ .../action_dispatch/testing/assertions/response.rb | 52 -------------- actionpack/lib/action_view/template.rb | 8 ++- actionpack/lib/action_view/test_case.rb | 1 + actionpack/test/abstract_unit.rb | 41 ----------- 5 files changed, 89 insertions(+), 95 deletions(-) diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index cdb5db32aa..107f70d3a3 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -2,6 +2,87 @@ require 'rack/session/abstract/id' require 'action_view/test_case' module ActionController + module TemplateAssertions + extend ActiveSupport::Concern + + included do + setup :setup_subscriptions + teardown :teardown_subscriptions + end + + def setup_subscriptions + @partials = Hash.new(0) + @templates = Hash.new(0) + ActiveSupport::Notifications.subscribe("action_view.slow_render_template") do |name, start, finish, id, payload| + path = payload[:virtual_path] + next unless path + partial = path =~ /^.*\/_[^\/]*$/ + if partial + @partials[path] += 1 + @partials[path.split("/").last] += 1 + @templates[path] += 1 + else + @templates[path] += 1 + end + end + end + + def teardown_subscriptions + ActiveSupport::Notifications.unsubscribe("action_view.slow_render_template") + end + + # 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" + # + # # 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) + validate_request! + + case options + when NilClass, String + rendered = @templates + msg = build_message(message, + "expecting but rendering with ", + options, rendered.keys.join(', ')) + assert_block(msg) do + if options.nil? + @templates.blank? + else + rendered.any? { |t,num| t.match(options) } + end + end + when Hash + if expected_partial = options[:partial] + if expected_count = options[:count] + actual_count = @partials[expected_partial] + # actual_count = found.nil? ? 0 : found[1] + msg = build_message(message, + "expecting ? to be rendered ? time(s) but rendered ? time(s)", + expected_partial, expected_count, actual_count) + assert(actual_count == expected_count.to_i, msg) + else + msg = build_message(message, + "expecting partial but action rendered ", + options[:partial], @partials.keys) + assert(@partials.include?(expected_partial), msg) + end + else + assert @partials.empty?, + "Expected no partials to be rendered" + end + end + end + end + class TestRequest < ActionDispatch::TestRequest #:nodoc: def initialize(env = {}) super @@ -181,6 +262,7 @@ module ActionController # assert_redirected_to page_url(:title => 'foo') class TestCase < ActiveSupport::TestCase include ActionDispatch::TestProcess + include ActionController::TemplateAssertions # Executes a request simulating GET HTTP method and set/volley the response def get(action, parameters = nil, session = nil, flash = nil) diff --git a/actionpack/lib/action_dispatch/testing/assertions/response.rb b/actionpack/lib/action_dispatch/testing/assertions/response.rb index 937c9f48d2..ec5e9efe44 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/response.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/response.rb @@ -73,58 +73,6 @@ module ActionDispatch end end - # 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" - # - # # 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) - validate_request! - - case options - when NilClass, String - rendered = (@controller.template.rendered[:template] || []).map { |t| t.identifier } - msg = build_message(message, - "expecting but rendering with ", - options, rendered.join(', ')) - assert_block(msg) do - if options.nil? - @controller.template.rendered[:template].blank? - else - rendered.any? { |t| t.match(options) } - end - end - when Hash - if expected_partial = options[:partial] - partials = @controller.template.rendered[:partials] - if expected_count = options[:count] - found = partials.detect { |p, _| p.identifier.match(expected_partial) } - actual_count = found.nil? ? 0 : found.second - msg = build_message(message, - "expecting ? to be rendered ? time(s) but rendered ? time(s)", - expected_partial, expected_count, actual_count) - assert(actual_count == expected_count.to_i, msg) - else - msg = build_message(message, - "expecting partial but action rendered ", - options[:partial], partials.keys) - assert(partials.keys.any? { |p| p.identifier.match(expected_partial) }, msg) - end - else - assert @controller.template.rendered[:partials].empty?, - "Expected no partials to be rendered" - end - end - end - private # Proxy to to_param if the object will respond to it. def parameterize(value) diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index 6315e502dd..ad37eb4c4a 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -36,8 +36,12 @@ module ActionView end def render(view, locals, &block) - method_name = compile(locals, view) - view.send(method_name, locals, &block) + # TODO: Revisit this name + # This is only slow if it's being listened to. Do not instrument this in production. + ActiveSupport::Notifications.instrument("action_view.slow_render_template", :virtual_path => @virtual_path) do + method_name = compile(locals, view) + view.send(method_name, locals, &block) + end rescue Exception => e if e.is_a?(Template::Error) e.sub_template_of(self) diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb index 7e609eb640..8750a501b7 100644 --- a/actionpack/lib/action_view/test_case.rb +++ b/actionpack/lib/action_view/test_case.rb @@ -42,6 +42,7 @@ module ActionView end include ActionDispatch::Assertions, ActionDispatch::TestProcess + include ActionController::TemplateAssertions include ActionView::Context include ActionController::PolymorphicRoutes diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 67aa412d3d..a3486aa039 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -239,47 +239,6 @@ module ActionController setup do @router = SharedTestRoutes end - - def assert_template(options = {}, message = nil) - validate_request! - - hax = @controller.view_context.instance_variable_get(:@_rendered) - - case options - when NilClass, String - rendered = (hax[:template] || []).map { |t| t.identifier } - msg = build_message(message, - "expecting but rendering with ", - options, rendered.join(', ')) - assert_block(msg) do - if options.nil? - hax[:template].blank? - else - rendered.any? { |t| t.match(options) } - end - end - when Hash - if expected_partial = options[:partial] - partials = hax[:partials] - if expected_count = options[:count] - found = partials.detect { |p, _| p.identifier.match(expected_partial) } - actual_count = found.nil? ? 0 : found[1] - msg = build_message(message, - "expecting ? to be rendered ? time(s) but rendered ? time(s)", - expected_partial, expected_count, actual_count) - assert(actual_count == expected_count.to_i, msg) - else - msg = build_message(message, - "expecting partial but action rendered ", - options[:partial], partials.keys) - assert(partials.keys.any? { |p| p.identifier.match(expected_partial) }, msg) - end - else - assert hax[:partials].empty?, - "Expected no partials to be rendered" - end - end - end end end -- cgit v1.2.3