From 314207677ef7eb7a9cfa6e45c4e4e5d95fc0b2da Mon Sep 17 00:00:00 2001 From: Paco Guzman Date: Sat, 15 May 2010 13:26:33 +0200 Subject: [PATCH] fix assert_select messages to its declaration behaviour --- .../action_dispatch/testing/assertions/selector.rb | 16 +++++++++++----- actionpack/test/controller/assert_select_test.rb | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/actionpack/lib/action_dispatch/testing/assertions/selector.rb b/actionpack/lib/action_dispatch/testing/assertions/selector.rb index a6b1126..9deabf5 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/selector.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/selector.rb @@ -298,10 +298,14 @@ module ActionDispatch # found one but expecting two. message ||= content_mismatch if matches.empty? # Test minimum/maximum occurrence. - 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 + min, max, count = equals[:minimum], equals[:maximum], equals[:count] + message = message || %(Expected #{count_description(min, max, count)} matching "#{selector.to_s}", found #{matches.size}.) + if count + assert matches.size == count, message + else + assert matches.size >= min, message if min + assert matches.size <= max, message if max + end # If a block is given call that block. Set @selected to allow # nested assert_select, which can be nested several levels deep. @@ -318,11 +322,13 @@ module ActionDispatch matches end - def count_description(min, max) #:nodoc: + def count_description(min, max, count) #:nodoc: pluralize = lambda {|word, quantity| word << (quantity == 1 ? '' : 's')} if min && max && (max != min) "between #{min} and #{max} elements" + elsif min && max && max == min && count + "exactly #{count} #{pluralize['element', min]}" elsif min && !(min == 1 && max == 1) "at least #{min} #{pluralize['element', min]}" elsif max diff --git a/actionpack/test/controller/assert_select_test.rb b/actionpack/test/controller/assert_select_test.rb index 4ef6fa4..f1254ab 100644 --- a/actionpack/test/controller/assert_select_test.rb +++ b/actionpack/test/controller/assert_select_test.rb @@ -80,10 +80,15 @@ class AssertSelectTest < ActionController::TestCase def test_assert_select render_html %Q{
} assert_select "div", 2 - 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 + def test_equality_integer + render_html %Q{
} + assert_failure(/Expected exactly 3 elements matching \"div\", found 2/) { assert_select "div", 3 } + assert_failure(/Expected exactly 0 elements matching \"div\", found 2/) { assert_select "div", 0 } + end + def test_equality_true_false render_html %Q{
} assert_nothing_raised { assert_select "div" } @@ -94,6 +99,11 @@ class AssertSelectTest < ActionController::TestCase assert_nothing_raised { assert_select "p", false } end + def test_equality_false_message + render_html %Q{
} + assert_failure(/Expected exactly 0 elements matching \"div\", found 2/) { assert_select "div", false } + end + def test_equality_string_and_regexp render_html %Q{
foo
foo
} assert_nothing_raised { assert_select "div", "foo" } @@ -128,7 +138,7 @@ class AssertSelectTest < ActionController::TestCase def test_counts render_html %Q{
foo
foo
} assert_nothing_raised { assert_select "div", 2 } - assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do + assert_failure(/Expected exactly 3 elements matching \"div\", found 2/) do assert_select "div", 3 end assert_nothing_raised { assert_select "div", 1..2 } @@ -136,7 +146,7 @@ class AssertSelectTest < ActionController::TestCase assert_select "div", 3..4 end assert_nothing_raised { assert_select "div", :count=>2 } - assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do + assert_failure(/Expected exactly 3 elements matching \"div\", found 2/) do assert_select "div", :count=>3 end assert_nothing_raised { assert_select "div", :minimum=>1 } -- 1.7.0.4