Skip to content

Commit 59a91c7

Browse files
committed
Fix #26 by running assertion when we're aren't testing for a body.
We're creating Nokogiri documents from HTML fragments, which means Nokogiri automatically adds a body element which is unexpected.
1 parent ba7265e commit 59a91c7

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/rails/dom/testing/assertions/selector_assertions.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ def assert_select(*args, &block)
165165
@selected ||= nil
166166

167167
selector = HTMLSelector.new(args, @selected) { nodeset document_root_element }
168+
169+
if selecting_no_body?(selector)
170+
assert true
171+
return
172+
end
173+
168174
selector.select.tap do |matches|
169175
assert_size_match!(matches.size, selector.tests, selector.selector, selector.message)
170176

@@ -276,6 +282,12 @@ def assert_size_match!(size, equals, css_selector, message = nil)
276282
end
277283
end
278284

285+
def selecting_no_body?(html_selector)
286+
# Nokogiri gives the document a body element. Which means we can't
287+
# run an assertion expecting there to not be a body.
288+
html_selector.selector == 'body' && html_selector.tests[:count] == 0
289+
end
290+
279291
def nest_selection(selection)
280292
# Set @selected to allow nested assert_select.
281293
# Can be nested several levels deep.

0 commit comments

Comments
 (0)