Skip to content

Commit

Permalink
Make assert_select's failure messages clearer about what failed. #7779
Browse files Browse the repository at this point in the history
…[dchelimsky]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6860 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
Marcel Molina committed May 26, 2007
1 parent 33e96f3 commit dc4d23f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *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] * 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] * auto_complete_field takes a :method option so you can GET or POST. #8120 [zapnap]
Expand Down
30 changes: 19 additions & 11 deletions actionpack/lib/action_controller/assertions/selector_assertions.rb
Expand Up @@ -196,7 +196,7 @@ def assert_select(*args, &block)
# Otherwise just operate on the response document. # Otherwise just operate on the response document.
root = response_from_page_or_rjs root = response_from_page_or_rjs
end end

# First or second argument is the selector: string and we pass # First or second argument is the selector: string and we pass
# all remaining arguments. Array and we pass the argument. Also # all remaining arguments. Array and we pass the argument. Also
# accepts selector itself. # accepts selector itself.
Expand All @@ -209,7 +209,7 @@ def assert_select(*args, &block)
selector = arg selector = arg
else raise ArgumentError, "Expecting a selector as the first argument" else raise ArgumentError, "Expecting a selector as the first argument"
end end

# Next argument is used for equality tests. # Next argument is used for equality tests.
equals = {} equals = {}
case arg = args.shift case arg = args.shift
Expand Down Expand Up @@ -277,14 +277,10 @@ def assert_select(*args, &block)
# found one but expecting two. # found one but expecting two.
message ||= content_mismatch if matches.empty? message ||= content_mismatch if matches.empty?
# Test minimum/maximum occurrence. # Test minimum/maximum occurrence.
if equals[:minimum] min, max = equals[:minimum], equals[:maximum]
assert matches.size >= equals[:minimum], message || message = message || %(Expected #{count_description(min, max)} matching "#{selector.to_s}", found #{matches.size}.)
"Expected at least #{equals[:minimum]} elements, found #{matches.size}." assert matches.size >= min, message if min
end assert matches.size <= max, message if max
if equals[:maximum]
assert matches.size <= equals[:maximum], message ||
"Expected at most #{equals[:maximum]} elements, found #{matches.size}."
end


# If a block is given call that block. Set @selected to allow # If a block is given call that block. Set @selected to allow
# nested assert_select, which can be nested several levels deep. # nested assert_select, which can be nested several levels deep.
Expand All @@ -300,7 +296,19 @@ def assert_select(*args, &block)
# Returns all matches elements. # Returns all matches elements.
matches matches
end 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: # :call-seq:
# assert_select_rjs(id?) { |elements| ... } # assert_select_rjs(id?) { |elements| ... }
# assert_select_rjs(statement, id?) { |elements| ... } # assert_select_rjs(statement, id?) { |elements| ... }
Expand Down
43 changes: 33 additions & 10 deletions actionpack/test/controller/assert_select_test.rb
Expand Up @@ -73,7 +73,12 @@ def setup
def teardown def teardown
ActionMailer::Base.deliveries.clear ActionMailer::Base.deliveries.clear
end 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. # Test assert select.
Expand All @@ -82,8 +87,8 @@ def teardown
def test_assert_select def test_assert_select
render_html %Q{<div id="1"></div><div id="2"></div>} render_html %Q{<div id="1"></div><div id="2"></div>}
assert_select "div", 2 assert_select "div", 2
assert_raises(AssertionFailedError) { assert_select "div", 3 } assert_failure(/Expected at least 3 elements matching \"div\", found 2/) { assert_select "div", 3 }
assert_raises(AssertionFailedError){ assert_select "p" } assert_failure(/Expected at least 1 element matching \"p\", found 0/) { assert_select "p" }
end end




Expand Down Expand Up @@ -131,22 +136,34 @@ def test_equality_of_html
end end




def test_equality_of_instances def test_counts
render_html %Q{<div id="1">foo</div><div id="2">foo</div>} render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
assert_nothing_raised { assert_select "div", 2 } 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_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_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=>1 }
assert_nothing_raised { assert_select "div", :minimum=>2 } 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=>2 }
assert_nothing_raised { assert_select "div", :maximum=>3 } 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_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 end




Expand Down Expand Up @@ -183,6 +200,12 @@ def test_nested_assert_select
assert_select "#3", false assert_select "#3", false
end end
end end

assert_failure(/Expected at least 1 element matching \"#4\", found 0\./) do
assert_select "div" do
assert_select "#4"
end
end
end end




Expand Down

0 comments on commit dc4d23f

Please sign in to comment.