Skip to content

Commit d0e26bf

Browse files
authored
Merge pull request rails#45748 from Shopify/action-view-refactor-output-buffer-test-2
Refactor Action View tests to stop re-assigning @output_buffer
2 parents df55109 + 4b67dff commit d0e26bf

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed

actionview/lib/action_view/buffers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def initialize(buffer = "")
2424
@raw_buffer.encode!
2525
end
2626

27-
delegate :length, :blank?, :encoding, :encode!, :force_encoding, to: :@raw_buffer
27+
delegate :length, :empty?, :blank?, :encoding, :encode!, :force_encoding, to: :@raw_buffer
2828

2929
def to_s
3030
@raw_buffer.html_safe

actionview/lib/action_view/context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module Context
1717
# Prepares the context by setting the appropriate instance variables.
1818
def _prepare_context
1919
@view_flow = OutputFlow.new
20-
@output_buffer = nil
20+
@output_buffer = ActionView::OutputBuffer.new
2121
@virtual_path = nil
2222
end
2323

actionview/test/activerecord/form_helper_activerecord_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class FormHelperActiveRecordTest < ActionView::TestCase
88
tests ActionView::Helpers::FormHelper
99

1010
def form_for(*)
11-
@output_buffer = super
11+
@rendered = super
1212
end
1313

1414
def setup
@@ -54,7 +54,7 @@ def test_nested_fields_for_with_child_index_option_override_on_a_nested_attribut
5454
'<input id="developer_projects_attributes_abc_id" name="developer[projects_attributes][abc][id]" type="hidden" value="321" autocomplete="off" />'
5555
end
5656

57-
assert_dom_equal expected, output_buffer
57+
assert_dom_equal expected, @rendered
5858
end
5959

6060
private

actionview/test/template/capture_helper_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ def setup
1010
end
1111

1212
def test_capture_captures_the_temporary_output_buffer_in_its_block
13-
assert_nil @av.output_buffer
13+
assert_predicate @av.output_buffer, :empty?
1414
string = @av.capture do
1515
@av.output_buffer << "foo"
1616
@av.output_buffer << "bar"
1717
end
18-
assert_nil @av.output_buffer
18+
assert_predicate @av.output_buffer, :empty?
1919
assert_equal "foobar", string
2020
end
2121

@@ -178,22 +178,22 @@ def test_provide
178178
end
179179

180180
def test_with_output_buffer_swaps_the_output_buffer_given_no_argument
181-
assert_nil @av.output_buffer
181+
assert_predicate @av.output_buffer, :empty?
182182
buffer = @av.with_output_buffer do
183183
@av.output_buffer << "."
184184
end
185185
assert_equal ".", buffer.to_s
186-
assert_nil @av.output_buffer
186+
assert_predicate @av.output_buffer, :empty?
187187
end
188188

189189
def test_with_output_buffer_swaps_the_output_buffer_with_an_argument
190-
assert_nil @av.output_buffer
190+
assert_predicate @av.output_buffer, :empty?
191191
buffer = ActionView::OutputBuffer.new(".")
192192
@av.with_output_buffer(buffer) do
193193
@av.output_buffer << "."
194194
end
195195
assert_equal "..", buffer.to_s
196-
assert_nil @av.output_buffer
196+
assert_predicate @av.output_buffer, :empty?
197197
end
198198

199199
def test_with_output_buffer_restores_the_output_buffer
@@ -218,7 +218,7 @@ def test_with_output_buffer_sets_proper_encoding
218218
end
219219

220220
def test_with_output_buffer_does_not_assume_there_is_an_output_buffer
221-
assert_nil @av.output_buffer
221+
assert_predicate @av.output_buffer, :empty?
222222
assert_equal "", @av.with_output_buffer { }.to_s
223223
end
224224

actionview/test/template/form_collections_helper_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ def assert_no_select(selector, value = nil)
1010
end
1111

1212
def with_collection_radio_buttons(*args, &block)
13-
@output_buffer = collection_radio_buttons(*args, &block)
13+
@rendered = collection_radio_buttons(*args, &block)
1414
end
1515

1616
def with_collection_check_boxes(*args, &block)
17-
@output_buffer = collection_check_boxes(*args, &block)
17+
@rendered = collection_check_boxes(*args, &block)
1818
end
1919

2020
# COLLECTION RADIO BUTTONS
@@ -197,7 +197,7 @@ def with_collection_check_boxes(*args, &block)
197197

198198
test "collection radio buttons with fields for" do
199199
collection = [Category.new(1, "Category 1"), Category.new(2, "Category 2")]
200-
@output_buffer = fields_for(:post) do |p|
200+
@rendered = fields_for(:post) do |p|
201201
p.collection_radio_buttons :category_id, collection, :id, :name
202202
end
203203

@@ -397,7 +397,7 @@ def with_collection_check_boxes(*args, &block)
397397
user = Struct.new(:category_ids).new(2)
398398
collection = (1..3).map { |i| [i, "Category #{i}"] }
399399

400-
@output_buffer = fields_for(:user, user) do |p|
400+
@rendered = fields_for(:user, user) do |p|
401401
p.collection_check_boxes :category_ids, collection, :first, :last, checked: [1, 3]
402402
end
403403

@@ -470,7 +470,7 @@ def with_collection_check_boxes(*args, &block)
470470

471471
test "collection check boxes with fields for" do
472472
collection = [Category.new(1, "Category 1"), Category.new(2, "Category 2")]
473-
@output_buffer = fields_for(:post) do |p|
473+
@rendered = fields_for(:post) do |p|
474474
p.collection_check_boxes :category_ids, collection, :id, :name
475475
end
476476

actionview/test/template/template_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def find_template(*args)
2121
class Context < ActionView::Base
2222
def initialize(*)
2323
super
24-
@output_buffer = "original"
24+
@output_buffer << "original"
2525
@virtual_path = nil
2626
end
2727

@@ -53,7 +53,7 @@ def logger
5353
end
5454

5555
def my_buffer
56-
@output_buffer
56+
@output_buffer.to_s
5757
end
5858
end
5959

0 commit comments

Comments
 (0)