Skip to content

Commit 085ef17

Browse files
committed
Better error message if response.body is blank or not parseable by Nokogiri
1 parent b97b9de commit 085ef17

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
module Rails
55
module Dom
66
module Testing
7+
MissingNodeError = Class.new(StandardError)
8+
79
module Assertions
810
# Adds the +assert_dom+ method for use in Rails functional
911
# test cases, which can be used to make assertions on the response HTML of a controller
@@ -296,8 +298,10 @@ def nest_selection(selection)
296298
def nodeset(node)
297299
if node.is_a?(Nokogiri::XML::NodeSet)
298300
node
299-
else
301+
elsif node.present?
300302
Nokogiri::XML::NodeSet.new(node.document, [node])
303+
else
304+
raise MissingNodeError, 'No node/document found. Did you return a blank body in your response?'
301305
end
302306
end
303307
end

test/selector_assertions_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,16 @@ def test_assert_select_with_extra_argument
322322
assert_select "title", text: "Welcome", count: 1
323323
end
324324

325+
def test_assert_select_on_blank_response
326+
render_html ""
327+
assert_raises Rails::Dom::Testing::MissingNodeError do
328+
assert_select "div", 0
329+
end
330+
assert_raises Rails::Dom::Testing::MissingNodeError do
331+
assert_select "div", 1
332+
end
333+
end
334+
325335
protected
326336
def render_html(html)
327337
fake_render(:html, html)

0 commit comments

Comments
 (0)