public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Add :partial option to assert_template [#1550 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
dust (author)
Thu Dec 11 09:06:35 -0800 2008
josh (committer)
Thu Dec 11 09:06:35 -0800 2008
commit  49306ccacf01e36d444771d42321965616e226f0
tree    bcd21d1a7e0ee6b6cbd80b1e9d1724b752e47a45
parent  5ede4ce188d29aef94af78f27d89169ac4ee54cd
...
16
17
18
19
 
20
21
22
...
41
42
43
44
 
45
46
47
...
60
61
62
63
 
64
65
66
67
68
 
69
70
71
...
75
76
77
78
 
79
80
81
82
83
84
85
 
 
 
 
 
 
 
86
87
88
89
90
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
93
 
 
94
95
96
97
98
99
100
101
102
103
...
16
17
18
 
19
20
21
22
...
41
42
43
 
44
45
46
47
...
60
61
62
 
63
64
65
66
67
 
68
69
70
71
...
75
76
77
 
78
79
80
81
82
83
84
 
85
86
87
88
89
90
91
92
 
 
 
 
 
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
 
123
124
125
126
127
128
129
130
 
131
132
133
0
@@ -16,7 +16,7 @@ module ActionController
0
       # ==== Examples
0
       #
0
       #   # assert that the response was a redirection
0
-      #   assert_response :redirect 
0
+      #   assert_response :redirect
0
       #
0
       #   # assert that the response code was status code 401 (unauthorized)
0
       #   assert_response 401
0
@@ -41,7 +41,7 @@ module ActionController
0
         end
0
       end
0
 
0
-      # Assert that the redirection options passed in match those of the redirect called in the latest action. 
0
+      # Assert that the redirection options passed in match those of the redirect called in the latest action.
0
       # This match can be partial, such that assert_redirected_to(:controller => "weblog") will also
0
       # match the redirection of redirect_to(:controller => "weblog", :action => "show") and so on.
0
       #
0
@@ -60,12 +60,12 @@ module ActionController
0
         clean_backtrace do
0
           assert_response(:redirect, message)
0
           return true if options == @response.redirected_to
0
-          
0
+
0
           # Support partial arguments for hash redirections
0
           if options.is_a?(Hash) && @response.redirected_to.is_a?(Hash)
0
             return true if options.all? {|(key, value)| @response.redirected_to[key] == value}
0
           end
0
-          
0
+
0
           redirected_to_after_normalisation = normalize_argument_to_redirection(@response.redirected_to)
0
           options_after_normalisation       = normalize_argument_to_redirection(options)
0
 
0
@@ -75,29 +75,59 @@ module ActionController
0
         end
0
       end
0
 
0
-      # Asserts that the request was rendered with the appropriate template file.
0
+      # Asserts that the request was rendered with the appropriate template file or partials
0
       #
0
       # ==== Examples
0
       #
0
       #   # assert that the "new" view template was rendered
0
       #   assert_template "new"
0
       #
0
-      def assert_template(expected = nil, message=nil)
0
+      #   # assert that the "_customer" partial was rendered twice
0
+      #   assert_template :partial => '_customer', :count => 2
0
+      #
0
+      #   # assert that no partials were rendered
0
+      #   assert_template :partial => false
0
+      #
0
+      def assert_template(options = {}, message = nil)
0
         clean_backtrace do
0
-          rendered = @response.rendered_template.to_s
0
-          msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered)
0
-          assert_block(msg) do
0
-            if expected.nil?
0
-              @response.rendered_template.blank?
0
+          case options
0
+           when NilClass, String
0
+            rendered = @response.rendered[:template].to_s
0
+            msg = build_message(message,
0
+                    "expecting <?> but rendering with <?>",
0
+                    options, rendered)
0
+            assert_block(msg) do
0
+              if options.nil?
0
+                @response.rendered[:template].blank?
0
+              else
0
+                rendered.to_s.match(options)
0
+              end
0
+            end
0
+          when Hash
0
+            if expected_partial = options[:partial]
0
+              partials = @response.rendered[:partials]
0
+              if expected_count = options[:count]
0
+                found = partials.detect { |p, _| p.to_s.match(expected_partial) }
0
+                actual_count = found.nil? ? 0 : found.second
0
+                msg = build_message(message,
0
+                        "expecting ? to be rendered ? time(s) but rendered ? time(s)",
0
+                         expected_partial, expected_count, actual_count)
0
+                assert(actual_count == expected_count.to_i, msg)
0
+              else
0
+                msg = build_message(message,
0
+                        "expecting partial <?> but action rendered <?>",
0
+                        options[:partial], partials.keys)
0
+                assert(partials.keys.any? { |p| p.to_s.match(expected_partial) }, msg)
0
+              end
0
             else
0
-              rendered.to_s.match(expected)
0
+              assert @response.rendered[:partials].empty?,
0
+                "Expected no partials to be rendered"
0
             end
0
           end
0
         end
0
       end
0
 
0
       private
0
-
0
         # Proxy to to_param if the object will respond to it.
0
         def parameterize(value)
0
           value.respond_to?(:to_param) ? value.to_param : value
...
221
222
223
224
225
 
 
226
227
228
...
221
222
223
 
 
224
225
226
227
228
0
@@ -221,8 +221,8 @@ module ActionController #:nodoc:
0
 
0
     # Returns the template of the file which was used to
0
     # render this response (or nil)
0
-    def rendered_template
0
-      template.instance_variable_get(:@_first_render)
0
+    def rendered
0
+      template.instance_variable_get(:@_rendered)
0
     end
0
 
0
     # A shortcut to the flash. Returns an empty hash if no session flash exists.
...
28
29
30
31
32
33
34
35
36
37
38
...
28
29
30
 
 
 
 
 
31
32
33
0
@@ -28,11 +28,6 @@ module ActionView
0
       stack = view.instance_variable_get(:@_render_stack)
0
       stack.push(self)
0
 
0
-      # This is only used for TestResponse to set rendered_template
0
-      unless is_a?(InlineTemplate) || view.instance_variable_get(:@_first_render)
0
-        view.instance_variable_set(:@_first_render, self)
0
-      end
0
-
0
       view.send(:_evaluate_assigns_and_ivars)
0
       view.send(:_set_controller_content_type, mime_type) if respond_to?(:mime_type)
0
 
...
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
3
4
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
0
@@ -1,4 +1,24 @@
0
 module ActionView
0
+  class Base
0
+    alias_method :initialize_without_template_tracking, :initialize
0
+    def initialize(*args)
0
+      @_rendered = { :template => nil, :partials => Hash.new(0) }
0
+      initialize_without_template_tracking(*args)
0
+    end
0
+  end
0
+
0
+  module Renderable
0
+    alias_method :render_without_template_tracking, :render
0
+    def render(view, local_assigns = {})
0
+      if respond_to?(:path) && !is_a?(InlineTemplate)
0
+        rendered = view.instance_variable_get(:@_rendered)
0
+        rendered[:partials][self] += 1 if is_a?(RenderablePartial)
0
+        rendered[:template] ||= self
0
+      end
0
+      render_without_template_tracking(view, local_assigns)
0
+    end
0
+  end
0
+
0
   class TestCase < ActiveSupport::TestCase
0
     include ActionController::TestCase::Assertions
0
 
...
326
327
328
329
 
330
331
332
333
 
 
334
335
336
...
326
327
328
 
329
330
331
 
 
332
333
334
335
336
0
@@ -326,11 +326,11 @@ class ActionPackAssertionsControllerTest < ActionController::TestCase
0
   # check if we were rendered by a file-based template?
0
   def test_rendered_action
0
     process :nothing
0
-    assert_nil @response.rendered_template
0
+    assert_nil @response.rendered[:template]
0
 
0
     process :hello_world
0
-    assert @response.rendered_template
0
-    assert 'hello_world', @response.rendered_template.to_s
0
+    assert @response.rendered[:template]
0
+    assert 'hello_world', @response.rendered[:template].to_s
0
   end
0
 
0
   # check the redirection location
...
1310
1311
1312
 
 
1313
1314
1315
1316
1317
 
 
1318
1319
1320
1321
1322
 
 
1323
1324
1325
1326
1327
 
1328
1329
1330
...
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
0
@@ -1310,21 +1310,28 @@ class RenderTest < ActionController::TestCase
0
   def test_partial_collection_with_spacer
0
     get :partial_collection_with_spacer
0
     assert_equal "Hello: davidonly partialHello: mary", @response.body
0
+    assert_template :partial => 'test/_partial_only'
0
+    assert_template :partial => '_customer'
0
   end
0
 
0
   def test_partial_collection_shorthand_with_locals
0
     get :partial_collection_shorthand_with_locals
0
     assert_equal "Bonjour: davidBonjour: mary", @response.body
0
+    assert_template :partial => 'customers/_customer', :count => 2
0
+    assert_template :partial => '_completely_fake_and_made_up_template_that_cannot_possibly_be_rendered', :count => 0
0
   end
0
 
0
   def test_partial_collection_shorthand_with_different_types_of_records
0
     get :partial_collection_shorthand_with_different_types_of_records
0
     assert_equal "Bonjour bad customer: mark0Bonjour good customer: craig1Bonjour bad customer: john2Bonjour good customer: zach3Bonjour good customer: brandon4Bonjour bad customer: dan5", @response.body
0
+    assert_template :partial => 'good_customers/_good_customer', :count => 3
0
+    assert_template :partial => 'bad_customers/_bad_customer', :count => 3
0
   end
0
 
0
   def test_empty_partial_collection
0
     get :empty_partial_collection
0
     assert_equal " ", @response.body
0
+    assert_template :partial => false
0
   end
0
 
0
   def test_partial_with_hash_object

Comments