aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_controller/assertions/selector_assertions.rb30
-rw-r--r--actionpack/test/controller/assert_select_test.rb43
3 files changed, 54 insertions, 21 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 19771aa462..9f36aebb49 100644
--- a/actionpack/CHANGELOG
+++ b/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]
diff --git a/actionpack/lib/action_controller/assertions/selector_assertions.rb b/actionpack/lib/action_controller/assertions/selector_assertions.rb
index 8de23c1f27..048bba153f 100644
--- a/actionpack/lib/action_controller/assertions/selector_assertions.rb
+++ b/actionpack/lib/action_controller/assertions/selector_assertions.rb
@@ -196,7 +196,7 @@ module ActionController
# 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 @@ module ActionController
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 @@ module ActionController
# 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 @@ module ActionController
# 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| ... }
diff --git a/actionpack/test/controller/assert_select_test.rb b/actionpack/test/controller/assert_select_test.rb
index f3b525ab92..36075a0871 100644
--- a/actionpack/test/controller/assert_select_test.rb
+++ b/actionpack/test/controller/assert_select_test.rb
@@ -73,7 +73,12 @@ class AssertSelectTest < Test::Unit::TestCase
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 @@ class AssertSelectTest < Test::Unit::TestCase
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 @@ class AssertSelectTest < Test::Unit::TestCase
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 @@ class AssertSelectTest < Test::Unit::TestCase
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