1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Include failed difference expression in assert message

This commit is contained in:
Jeremy Kemper 2009-02-06 09:55:18 -08:00
parent 8746f7cac2
commit 7564d98929
2 changed files with 15 additions and 10 deletions

View file

@ -31,16 +31,18 @@ module ActiveSupport
# 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 @@ module ActiveSupport
# 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

View file

@ -66,7 +66,8 @@ class AssertDifferenceTest < ActiveSupport::TestCase
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 @@ class AssertDifferenceTest < ActiveSupport::TestCase
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