aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authormadlep <madlep@ubercharged.net>2008-10-03 11:58:28 +1000
committerMichael Koziarski <michael@koziarski.com>2008-10-03 21:31:56 +0200
commit00e2ba76b21d3c433a0ecda0de28d931c34d1791 (patch)
treee6af86c9303af0ae08e1c59b90ecf2b1c5d14d2d /activesupport
parent395369bc2b85346e8f196f202941e5015dc1481e (diff)
downloadrails-00e2ba76b21d3c433a0ecda0de28d931c34d1791.tar.gz
rails-00e2ba76b21d3c433a0ecda0de28d931c34d1791.tar.bz2
rails-00e2ba76b21d3c433a0ecda0de28d931c34d1791.zip
added nicer failure reporting to #assert_difference to tell you the expression that failed rather than just the expected and actual values
Signed-off-by: Michael Koziarski <michael@koziarski.com> [#1161 state:committed]
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/testing/core_ext/test/unit/assertions.rb11
-rw-r--r--activesupport/test/test_test.rb18
2 files changed, 25 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/testing/core_ext/test/unit/assertions.rb b/activesupport/lib/active_support/testing/core_ext/test/unit/assertions.rb
index 63d1ba6507..e5853bf828 100644
--- a/activesupport/lib/active_support/testing/core_ext/test/unit/assertions.rb
+++ b/activesupport/lib/active_support/testing/core_ext/test/unit/assertions.rb
@@ -37,15 +37,18 @@ module Test
# end
def assert_difference(expressions, difference = 1, message = nil, &block)
expression_evaluations = Array(expressions).map do |expression|
- lambda do
+ [expression, lambda do
eval(expression, block.__send__(:binding))
- end
+ end]
end
- original_values = expression_evaluations.inject([]) { |memo, expression| memo << expression.call }
+ original_values = expression_evaluations.inject([]) { |memo, expression| memo << expression[1].call }
yield
expression_evaluations.each_with_index do |expression, i|
- assert_equal original_values[i] + difference, expression.call, message
+ full_message = ""
+ full_message << "#{message}.\n" if message
+ full_message << "<#{expression[0]}> was the expression that failed"
+ assert_equal original_values[i] + difference, expression[1].call, full_message
end
end
diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb
index 26a45af255..dd1bc8d623 100644
--- a/activesupport/test/test_test.rb
+++ b/activesupport/test/test_test.rb
@@ -60,6 +60,24 @@ class AssertDifferenceTest < Test::Unit::TestCase
@object.increment
end
end
+
+ def test_array_of_expressions_identify_failure
+ assert_difference ['@object.num', '1 + 1'] do
+ @object.increment
+ end
+ fail 'should not get to here'
+ rescue Test::Unit::AssertionFailedError => e
+ assert_equal "<1 + 1> was expression that failed.\n<3> expected but was\n<2>.", e.message
+ end
+
+ def test_array_of_expressions_identify_failure_when_message_provided
+ assert_difference ['@object.num', '1 + 1'], 1, 'something went wrong' do
+ @object.increment
+ end
+ fail 'should not get to here'
+ rescue Test::Unit::AssertionFailedError => e
+ assert_equal "something went wrong.\n<1 + 1> was expression that failed.\n<3> expected but was\n<2>.", e.message
+ end
else
def default_test; end
end