diff --git a/lib/rails/dom/testing/assertions/selector_assertions.rb b/lib/rails/dom/testing/assertions/selector_assertions.rb index 18602d0..41fcb75 100644 --- a/lib/rails/dom/testing/assertions/selector_assertions.rb +++ b/lib/rails/dom/testing/assertions/selector_assertions.rb @@ -191,11 +191,7 @@ def assert_not_dom(*args, &block) alias_method :refute_select, :assert_not_dom private def dom_assertions(selector, &block) - if selector.selecting_no_body? - assert true - return - end - + selector.ensure_no_assertion_on_nokogiri_body! count, max = selector.tests.slice(:count, :maximum).values selector.select.tap do |matches| diff --git a/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb b/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb index b3bcce2..c478222 100644 --- a/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb +++ b/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb @@ -30,10 +30,10 @@ def initialize(values, previous_selection = nil, refute: false, &root_fallback) end end - def selecting_no_body? # :nodoc: - # Nokogiri gives the document a body element. Which means we can't - # run an assertion expecting there to not be a body. - @selector == "body" && @tests[:count] == 0 + def ensure_no_assertion_on_nokogiri_body! # :nodoc: + # Nokogiri gives the document a body element when missing + return unless @selector == "body" && (@tests.keys & [:text, :html]).none? + raise ArgumentError, "Assertions on body can only be for text or html" end def select diff --git a/test/selector_assertions_test.rb b/test/selector_assertions_test.rb index 6797495..acf3d99 100644 --- a/test/selector_assertions_test.rb +++ b/test/selector_assertions_test.rb @@ -477,7 +477,8 @@ def test_feed_item_encoded_with_html_version def test_body_not_present_in_empty_document render_html "
" - assert_select "body", 0 + error = assert_raises(ArgumentError) { assert_select "body", 0 } + assert_equal "Assertions on body can only be for text or html", error.message end def test_body_class_can_be_tested @@ -490,6 +491,17 @@ def test_body_class_can_be_tested_with_html assert_select ".foo" end + def test_assert_not_select_on_body_with_text + render_html "foo
bar