Skip to content
Browse files

Make assert_select's failure messages clearer about what failed. #7779

…[dchelimsky]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6860 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 33e96f3 commit dc4d23f2d736a81be36edb0abf42774634741bf3 Marcel Molina committed
View
2 actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Make assert_select's failure messages clearer about what failed. #7779 [dchelimsky]
+
* Introduce a default respond_to block for custom types. #8174 [Josh Peek]
* auto_complete_field takes a :method option so you can GET or POST. #8120 [zapnap]
View
30 actionpack/lib/action_controller/assertions/selector_assertions.rb
@@ -196,7 +196,7 @@ def assert_select(*args, &block)
# Otherwise just operate on the response document.
root = response_from_page_or_rjs
end
-
+
# First or second argument is the selector: string and we pass
# all remaining arguments. Array and we pass the argument. Also
# accepts selector itself.
@@ -209,7 +209,7 @@ def assert_select(*args, &block)
selector = arg
else raise ArgumentError, "Expecting a selector as the first argument"
end
-
+
# Next argument is used for equality tests.
equals = {}
case arg = args.shift
@@ -277,14 +277,10 @@ def assert_select(*args, &block)
# found one but expecting two.
message ||= content_mismatch if matches.empty?
# Test minimum/maximum occurrence.
- if equals[:minimum]
- assert matches.size >= equals[:minimum], message ||
- "Expected at least #{equals[:minimum]} elements, found #{matches.size}."
- end
- if equals[:maximum]
- assert matches.size <= equals[:maximum], message ||
- "Expected at most #{equals[:maximum]} elements, found #{matches.size}."
- end
+ min, max = equals[:minimum], equals[:maximum]
+ message = message || %(Expected #{count_description(min, max)} matching "#{selector.to_s}", found #{matches.size}.)
+ assert matches.size >= min, message if min
+ assert matches.size <= max, message if max
# If a block is given call that block. Set @selected to allow
# nested assert_select, which can be nested several levels deep.
@@ -300,7 +296,19 @@ def assert_select(*args, &block)
# Returns all matches elements.
matches
end
-
+
+ def count_description(min, max) #:nodoc:
+ pluralize = lambda {|word, quantity| word << (quantity == 1 ? '' : 's')}
+
+ if min && max && (max != min)
+ "between #{min} and #{max} elements"
+ elsif min && !(min == 1 && max == 1)
+ "at least #{min} #{pluralize['element', min]}"
+ elsif max
+ "at most #{max} #{pluralize['element', max]}"
+ end
+ end
+
# :call-seq:
# assert_select_rjs(id?) { |elements| ... }
# assert_select_rjs(statement, id?) { |elements| ... }
View
43 actionpack/test/controller/assert_select_test.rb
@@ -73,7 +73,12 @@ def setup
def teardown
ActionMailer::Base.deliveries.clear
end
-
+
+ def assert_failure(message, &block)
+ e = assert_raises(AssertionFailedError, &block)
+ assert_match(message, e.message) if Regexp === message
+ assert_equal(message, e.message) if String === message
+ end
#
# Test assert select.
@@ -82,8 +87,8 @@ def teardown
def test_assert_select
render_html %Q{<div id="1"></div><div id="2"></div>}
assert_select "div", 2
- assert_raises(AssertionFailedError) { assert_select "div", 3 }
- assert_raises(AssertionFailedError){ assert_select "p" }
+ assert_failure(/Expected at least 3 elements matching \"div\", found 2/) { assert_select "div", 3 }
+ assert_failure(/Expected at least 1 element matching \"p\", found 0/) { assert_select "p" }
end
@@ -131,22 +136,34 @@ def test_equality_of_html
end
- def test_equality_of_instances
+ def test_counts
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
assert_nothing_raised { assert_select "div", 2 }
- assert_raises(AssertionFailedError) { assert_select "div", 3 }
+ assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
+ assert_select "div", 3
+ end
assert_nothing_raised { assert_select "div", 1..2 }
- assert_raises(AssertionFailedError) { assert_select "div", 3..4 }
+ assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
+ assert_select "div", 3..4
+ end
assert_nothing_raised { assert_select "div", :count=>2 }
- assert_raises(AssertionFailedError) { assert_select "div", :count=>3 }
+ assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
+ assert_select "div", :count=>3
+ end
assert_nothing_raised { assert_select "div", :minimum=>1 }
assert_nothing_raised { assert_select "div", :minimum=>2 }
- assert_raises(AssertionFailedError) { assert_select "div", :minimum=>3 }
+ assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
+ assert_select "div", :minimum=>3
+ end
assert_nothing_raised { assert_select "div", :maximum=>2 }
assert_nothing_raised { assert_select "div", :maximum=>3 }
- assert_raises(AssertionFailedError) { assert_select "div", :maximum=>1 }
+ assert_failure(/Expected at most 1 element matching \"div\", found 2/) do
+ assert_select "div", :maximum=>1
+ end
assert_nothing_raised { assert_select "div", :minimum=>1, :maximum=>2 }
- assert_raises(AssertionFailedError) { assert_select "div", :minimum=>3, :maximum=>4 }
+ assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
+ assert_select "div", :minimum=>3, :maximum=>4
+ end
end
@@ -183,6 +200,12 @@ def test_nested_assert_select
assert_select "#3", false
end
end
+
+ assert_failure(/Expected at least 1 element matching \"#4\", found 0\./) do
+ assert_select "div" do
+ assert_select "#4"
+ end
+ end
end

0 comments on commit dc4d23f

Please sign in to comment.
Something went wrong with that request. Please try again.