From 7564d98929af0bb039cb10c59dff6d177c11bd13 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Feb 2009 09:55:18 -0800 Subject: [PATCH] Include failed difference expression in assert message --- .../lib/active_support/testing/assertions.rb | 18 ++++++++++-------- activesupport/test/test_test.rb | 7 +++++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb index 7b19e3f35ff1..771c116dff0d 100644 --- a/activesupport/lib/active_support/testing/assertions.rb +++ b/activesupport/lib/active_support/testing/assertions.rb @@ -31,16 +31,18 @@ module Assertions # assert_difference 'Article.count', -1, "An Article should be destroyed" do # post :delete, :id => ... # end - def assert_difference(expressions, difference = 1, message = nil, &block) - case expressions + def assert_difference(expression, difference = 1, message = nil, &block) + case expression when String - before = eval(expressions, block.send(:binding)) + before = eval(expression, block.send(:binding)) yield - assert_equal(before + difference, eval(expressions, block.send(:binding)), message) + error = "#{expression.inspect} didn't change by #{difference}" + error = "#{message}.\n#{error}" if message + assert_equal(before + difference, eval(expression, block.send(:binding)), error) when Enumerable - expressions.each { |e| assert_difference(e, difference, message, &block) } + expression.each { |e| assert_difference(e, difference, message, &block) } else - raise ArgumentError, "Unrecognized expression: #{expressions.inspect}" + raise ArgumentError, "Unrecognized expression: #{expression.inspect}" end end @@ -56,8 +58,8 @@ def assert_difference(expressions, difference = 1, message = nil, &block) # assert_no_difference 'Article.count', "An Article should not be destroyed" do # post :create, :article => invalid_attributes # end - def assert_no_difference(expressions, message = nil, &block) - assert_difference expressions, 0, message, &block + def assert_no_difference(expression, message = nil, &block) + assert_difference expression, 0, message, &block end end end diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index 298037e27c48..d250b1082203 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -66,7 +66,8 @@ def test_array_of_expressions_identify_failure end fail 'should not get to here' rescue Exception => e - assert_equal "<3> expected but was\n<2>.", e.message + assert_match(/didn't change by/, e.message) + assert_match(/expected but was/, e.message) end def test_array_of_expressions_identify_failure_when_message_provided @@ -75,7 +76,9 @@ def test_array_of_expressions_identify_failure_when_message_provided end fail 'should not get to here' rescue Exception => e - assert_equal "something went wrong.\n<3> expected but was\n<2>.", e.message + assert_match(/something went wrong/, e.message) + assert_match(/didn't change by/, e.message) + assert_match(/expected but was/, e.message) end else def default_test; end