Skip to content

Commit 75a8efe

Browse files
committed
Raise an error when given a block with a 0 element assertion
1 parent 5fd54c2 commit 75a8efe

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,19 @@ def assert_not_dom(*args, &block)
196196
return
197197
end
198198

199+
count, max = selector.tests.slice(:count, :maximum).values
200+
199201
selector.select.tap do |matches|
200202
assert_size_match!(matches.size, selector.tests,
201203
selector.css_selector, selector.message)
202204

203-
nest_selection(matches, &block) if block_given? && !matches.empty?
205+
if block_given?
206+
if count&.zero? || max&.zero?
207+
raise ArgumentError, "Cannot be called with a block when asserting that a selector does not match"
208+
end
209+
210+
nest_selection(matches, &block) unless matches.empty?
211+
end
204212
end
205213
end
206214

test/selector_assertions_test.rb

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ def test_counts
122122
end
123123
end
124124

125+
def test_zero_counts_with_block
126+
render_html "<div>foo</div>"
127+
128+
errors = [
129+
assert_raises(ArgumentError) { assert_select("p", false) { nil } },
130+
assert_raises(ArgumentError) { assert_select("p", 0) { nil } },
131+
assert_raises(ArgumentError) { assert_select("p", count: 0) { nil } },
132+
assert_raises(ArgumentError) { assert_select("p", 0..0) { nil } },
133+
assert_raises(ArgumentError) { assert_select("p", minimum: 0, maximum: 0) { nil } }
134+
]
135+
assert_equal ["Cannot be called with a block when asserting that a selector does not match"], errors.map(&:message).uniq
136+
end
137+
125138
def test_substitution_values
126139
render_html '<div id="1">foo</div><div id="2">foo</div>'
127140
assert_select "div:match('id', ?)", /\d+/ do |elements|
@@ -214,46 +227,59 @@ def test_assert_select_text_match
214227
# Test assert_not_select.
215228
#
216229

217-
def test_not_select
230+
def test_assert_not_select
218231
render_html '<div id="1"></div>'
219232
assert_not_select "p"
220233
assert_failure(/Expected exactly 0 elements matching "div", found 1/) { assert_not_select "div" }
221234
assert_failure(/Expected exactly 0 elements matching "div#1", found 1/) { assert_not_select "div#1" }
222235
end
223236

224-
def test_not_select_with_true
237+
def test_assert_not_select_with_true
225238
render_html '<div id="1"></div>'
226-
assert_raises(ArgumentError) { assert_not_select "div", true }
239+
error = assert_raises(ArgumentError) { assert_not_select "div", true }
240+
assert_equal "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match", error.message
227241
end
228242

229-
def test_not_select_with_false
243+
def test_assert_not_select_with_false
230244
render_html '<div id="1"></div>'
231-
assert_raises(ArgumentError) { assert_not_select "div", false }
245+
error = assert_raises(ArgumentError) { assert_not_select "div", false }
246+
assert_equal "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match", error.message
232247
end
233248

234-
def test_not_select_with_integer
249+
def test_assert_not_select_with_integer
235250
render_html '<div id="1"></div>'
236-
assert_raises(ArgumentError) { assert_not_select "div", 1 }
251+
error = assert_raises(ArgumentError) { assert_not_select "div", 1 }
252+
assert_equal "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match", error.message
237253
end
238254

239-
def test_not_select_with_range
255+
def test_assert_not_select_with_range
240256
render_html '<div id="1"></div>'
241-
assert_raises(ArgumentError) { assert_not_select "div", 1..5 }
257+
error = assert_raises(ArgumentError) { assert_not_select "div", 1..5 }
258+
assert_equal "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match", error.message
242259
end
243260

244-
def test_not_select_with_count
261+
def test_assert_not_select_with_count
245262
render_html '<div id="1"></div>'
246-
assert_raises(ArgumentError) { assert_not_select "div", count: 1 }
263+
error = assert_raises(ArgumentError) { assert_not_select "div", count: 1 }
264+
assert_equal "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match", error.message
247265
end
248266

249-
def test_not_select_with_minimum
267+
def test_assert_not_select_with_minimum
250268
render_html '<div id="1"></div>'
251-
assert_raises(ArgumentError) { assert_not_select "div", minimum: 1 }
269+
error = assert_raises(ArgumentError) { assert_not_select "div", minimum: 1 }
270+
assert_equal "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match", error.message
252271
end
253272

254-
def test_not_select_with_maximum
273+
def test_assert_not_select_with_maximum
255274
render_html '<div id="1"></div>'
256-
assert_raises(ArgumentError) { assert_not_select "div", maximum: 1 }
275+
error = assert_raises(ArgumentError) { assert_not_select "div", maximum: 1 }
276+
assert_equal "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match", error.message
277+
end
278+
279+
def test_assert_not_select_with_block
280+
render_html "<div>foo</div>"
281+
error = assert_raises(ArgumentError) { assert_not_select("p") { nil } }
282+
assert_equal "Cannot be called with a block when asserting that a selector does not match", error.message
257283
end
258284

259285
#

0 commit comments

Comments
 (0)