diff --git a/lib/rails/controller/testing/template_assertions.rb b/lib/rails/controller/testing/template_assertions.rb index 5a4c29f..af35343 100644 --- a/lib/rails/controller/testing/template_assertions.rb +++ b/lib/rails/controller/testing/template_assertions.rb @@ -11,7 +11,7 @@ module TemplateAssertions teardown :teardown_subscriptions end - RENDER_TEMPLATE_INSTANCE_VARIABLES = %w{partials templates layouts files}.freeze + RENDER_TEMPLATE_INSTANCE_VARIABLES = %w{partials templates layouts files identifiers}.freeze def setup_subscriptions RENDER_TEMPLATE_INSTANCE_VARIABLES.each do |instance_variable| @@ -31,6 +31,11 @@ def setup_subscriptions end @_subscribers << ActiveSupport::Notifications.subscribe("!render_template.action_view") do |_name, _start, _finish, _id, payload| + path = payload[:identifier] + if path + @_identifiers[path] += 1 + end + if virtual_path = payload[:virtual_path] partial = virtual_path =~ /^.*\/_[^\/]*$/ @@ -40,12 +45,9 @@ def setup_subscriptions end @_templates[virtual_path] += 1 - else - path = payload[:identifier] - if path - @_files[path] += 1 - @_files[path.split("/").last] += 1 - end + elsif path + @_files[path] += 1 + @_files[path.split("/").last] += 1 end end end @@ -101,6 +103,9 @@ def reset_template_assertion # assert_template file: nil # assert_template file: false # + # # assert that the template "/path/to/app/views/admin/posts/new.html.erb" was rendered + # assert_template identifier: "/path/to/app/views/admin/posts/new.html.erb" + # # In a view test case, you can also assert that specific locals are passed # to partials: # @@ -131,7 +136,7 @@ def assert_template(options = {}, message = nil) end assert matches_template, msg when Hash - options.assert_valid_keys(:layout, :partial, :locals, :count, :file) + options.assert_valid_keys(:layout, :partial, :locals, :count, :file, :identifier) if options.key?(:layout) expected_layout = options[:layout] @@ -156,6 +161,10 @@ def assert_template(options = {}, message = nil) assert @_files.blank?, "expected no files but #{@_files.keys} was rendered" end + if options[:identifier] + assert_includes @_identifiers.keys, options[:identifier] + end + if expected_partial = options[:partial] if expected_locals = options[:locals] if defined?(@_rendered_views) diff --git a/test/controllers/template_assertions_test.rb b/test/controllers/template_assertions_test.rb index d1a6fa3..41ea38c 100644 --- a/test/controllers/template_assertions_test.rb +++ b/test/controllers/template_assertions_test.rb @@ -160,6 +160,27 @@ def test_passes_with_correct_layout_symbol assert_template layout: :standard end + def test_identifier + get :render_with_template + assert_template identifier: Rails.root.join('app/views/test/hello_world.html.erb').to_s + end + + def test_identifier_with_layout + get :render_with_layout + assert_template identifier: Rails.root.join('app/views/layouts/standard.html.erb').to_s + end + + def test_identifier_with_partial + get :render_with_partial + assert_template identifier: Rails.root.join('app/views/test/_partial.html.erb').to_s + end + + def test_identifier_with_layout_and_partial + get :render_with_layout_and_partial + assert_template identifier: Rails.root.join('app/views/layouts/standard.html.erb').to_s + assert_template identifier: Rails.root.join('app/views/test/_partial.html.erb').to_s + end + def test_assert_template_reset_between_requests get :render_with_template assert_template 'test/hello_world'