Skip to content
This repository
Browse code

test helpers in erb using erb

  • Loading branch information...
commit e975fe710a1a78d723cfd91c07014627f92e3250 1 parent 271308c
Sergey Nartimov authored November 30, 2011
8  actionpack/test/abstract_unit.rb
@@ -73,6 +73,14 @@ def body_to_string(body)
73 73
 end
74 74
 
75 75
 module RenderERBUtils
  76
+  def view
  77
+    @view ||= begin
  78
+      path = ActionView::FileSystemResolver.new(FIXTURE_LOAD_PATH)
  79
+      view_paths = ActionView::PathSet.new([path])
  80
+      ActionView::Base.new(view_paths)
  81
+    end
  82
+  end
  83
+
76 84
   def render_erb(string)
77 85
     template = ActionView::Template.new(
78 86
       string.strip,
3  actionpack/test/fixtures/test/_content_tag_nested_in_content_tag.erb
... ...
@@ -0,0 +1,3 @@
  1
+<%= content_tag 'p' do %>
  2
+  <%= content_tag 'b', 'Hello' %>
  3
+<% end %>
5  actionpack/test/template/form_helper_test.rb
@@ -3,6 +3,8 @@
3 3
 require 'active_support/core_ext/object/inclusion'
4 4
 
5 5
 class FormHelperTest < ActionView::TestCase
  6
+  include RenderERBUtils
  7
+
6 8
   tests ActionView::Helpers::FormHelper
7 9
 
8 10
   def form_for(*)
@@ -231,9 +233,6 @@ def test_label_with_block
231 233
   end
232 234
 
233 235
   def test_label_with_block_in_erb
234  
-    path = ActionView::FileSystemResolver.new(FIXTURE_LOAD_PATH)
235  
-    view_paths = ActionView::PathSet.new([path])
236  
-    view = ActionView::Base.new(view_paths)
237 236
     assert_equal "<label for=\"post_message\">\n  Message\n  <input id=\"post_message\" name=\"post[message]\" size=\"30\" type=\"text\" />\n</label>", view.render("test/label_with_block")
238 237
   end
239 238
 
16  actionpack/test/template/form_tag_helper_test.rb
@@ -2,6 +2,8 @@
2 2
 require 'active_support/core_ext/object/inclusion'
3 3
 
4 4
 class FormTagHelperTest < ActionView::TestCase
  5
+  include RenderERBUtils
  6
+
5 7
   tests ActionView::Helpers::FormTagHelper
6 8
 
7 9
   def setup
@@ -104,14 +106,14 @@ def test_form_tag_with_remote_false
104 106
   end
105 107
 
106 108
   def test_form_tag_with_block_in_erb
107  
-    output_buffer = form_tag("http://www.example.com") { concat "Hello world!" }
  109
+    output_buffer = render_erb("<%= form_tag('http://www.example.com') do %>Hello world!<% end %>")
108 110
 
109 111
     expected = whole_form { "Hello world!" }
110 112
     assert_dom_equal expected, output_buffer
111 113
   end
112 114
 
113 115
   def test_form_tag_with_block_and_method_in_erb
114  
-    output_buffer = form_tag("http://www.example.com", :method => :put) { concat "Hello world!" }
  116
+    output_buffer = render_erb("<%= form_tag('http://www.example.com', :method => :put) do %>Hello world!<% end %>")
115 117
 
116 118
     expected = whole_form("http://www.example.com", :method => "put") do
117 119
       "Hello world!"
@@ -485,27 +487,27 @@ def test_pass
485 487
   end
486 488
 
487 489
   def test_field_set_tag_in_erb
488  
-    output_buffer = field_set_tag("Your details") { concat "Hello world!" }
  490
+    output_buffer = render_erb("<%= field_set_tag('Your details') do %>Hello world!<% end %>")
489 491
 
490 492
     expected = %(<fieldset><legend>Your details</legend>Hello world!</fieldset>)
491 493
     assert_dom_equal expected, output_buffer
492 494
 
493  
-    output_buffer = field_set_tag { concat "Hello world!" }
  495
+    output_buffer = render_erb("<%= field_set_tag do %>Hello world!<% end %>")
494 496
 
495 497
     expected = %(<fieldset>Hello world!</fieldset>)
496 498
     assert_dom_equal expected, output_buffer
497 499
 
498  
-    output_buffer = field_set_tag('') { concat "Hello world!" }
  500
+    output_buffer = render_erb("<%= field_set_tag('') do %>Hello world!<% end %>")
499 501
 
500 502
     expected = %(<fieldset>Hello world!</fieldset>)
501 503
     assert_dom_equal expected, output_buffer
502 504
 
503  
-    output_buffer = field_set_tag('', :class => 'format') { concat "Hello world!" }
  505
+    output_buffer = render_erb("<%= field_set_tag('', :class => 'format') do %>Hello world!<% end %>")
504 506
 
505 507
     expected = %(<fieldset class="format">Hello world!</fieldset>)
506 508
     assert_dom_equal expected, output_buffer
507 509
   end
508  
-  
  510
+
509 511
   def test_text_area_tag_options_symbolize_keys_side_effects
510 512
     options = { :option => "random_option" }
511 513
     text_area_tag "body", "hello world", options
6  actionpack/test/template/record_tag_helper_test.rb
@@ -22,6 +22,8 @@ def body
22 22
 end
23 23
 
24 24
 class RecordTagHelperTest < ActionView::TestCase
  25
+  include RenderERBUtils
  26
+
25 27
   tests ActionView::Helpers::RecordTagHelper
26 28
 
27 29
   def setup
@@ -58,13 +60,13 @@ def test_block_not_in_erb_multiple_calls
58 60
 
59 61
   def test_block_works_with_content_tag_for_in_erb
60 62
     expected = %(<tr class="post" id="post_45">#{@post.body}</tr>)
61  
-    actual = content_tag_for(:tr, @post) { concat @post.body }
  63
+    actual = render_erb("<%= content_tag_for(:tr, @post) do %><%= @post.body %><% end %>")
62 64
     assert_dom_equal expected, actual
63 65
   end
64 66
 
65 67
   def test_div_for_in_erb
66 68
     expected = %(<div class="post bar" id="post_45">#{@post.body}</div>)
67  
-    actual = div_for(@post, :class => "bar") { concat @post.body }
  69
+    actual = render_erb("<%= div_for(@post, :class => 'bar') do %><%= @post.body %><% end %>")
68 70
     assert_dom_equal expected, actual
69 71
   end
70 72
 
10  actionpack/test/template/tag_helper_test.rb
... ...
@@ -1,6 +1,8 @@
1 1
 require 'abstract_unit'
2 2
 
3 3
 class TagHelperTest < ActionView::TestCase
  4
+  include RenderERBUtils
  5
+
4 6
   tests ActionView::Helpers::TagHelper
5 7
 
6 8
   def test_tag
@@ -44,12 +46,12 @@ def test_content_tag
44 46
   end
45 47
 
46 48
   def test_content_tag_with_block_in_erb
47  
-    buffer = content_tag(:div) { concat "Hello world!" }
  49
+    buffer = render_erb("<%= content_tag(:div) do %>Hello world!<% end %>")
48 50
     assert_dom_equal "<div>Hello world!</div>", buffer
49 51
   end
50 52
 
51 53
   def test_content_tag_with_block_and_options_in_erb
52  
-    buffer = content_tag(:div, :class => "green") { concat "Hello world!" }
  54
+    buffer = render_erb("<%= content_tag(:div, :class => 'green') do %>Hello world!<% end %>")
53 55
     assert_dom_equal %(<div class="green">Hello world!</div>), buffer
54 56
   end
55 57
 
@@ -68,10 +70,8 @@ def test_content_tag_nested_in_content_tag_out_of_erb
68 70
                  output_buffer
69 71
   end
70 72
 
71  
-  # TAG TODO: Move this into a real template
72 73
   def test_content_tag_nested_in_content_tag_in_erb
73  
-    buffer = content_tag("p") { concat content_tag("b", "Hello") }
74  
-    assert_equal '<p><b>Hello</b></p>', buffer
  74
+    assert_equal "<p>\n  <b>Hello</b>\n</p>", view.render("test/content_tag_nested_in_content_tag")
75 75
   end
76 76
 
77 77
   def test_content_tag_with_escaped_array_class

0 notes on commit e975fe7

Please sign in to comment.
Something went wrong with that request. Please try again.