Skip to content

Commit

Permalink
Include failed difference expression in assert message
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Feb 6, 2009
1 parent 8746f7c commit 7564d98
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
18 changes: 10 additions & 8 deletions activesupport/lib/active_support/testing/assertions.rb
Expand Up @@ -31,16 +31,18 @@ module Assertions
# assert_difference 'Article.count', -1, "An Article should be destroyed" do # assert_difference 'Article.count', -1, "An Article should be destroyed" do
# post :delete, :id => ... # post :delete, :id => ...
# end # end
def assert_difference(expressions, difference = 1, message = nil, &block) def assert_difference(expression, difference = 1, message = nil, &block)
case expressions case expression
when String when String
before = eval(expressions, block.send(:binding)) before = eval(expression, block.send(:binding))
yield 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 when Enumerable
expressions.each { |e| assert_difference(e, difference, message, &block) } expression.each { |e| assert_difference(e, difference, message, &block) }
else else
raise ArgumentError, "Unrecognized expression: #{expressions.inspect}" raise ArgumentError, "Unrecognized expression: #{expression.inspect}"
end end
end end


Expand All @@ -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 # assert_no_difference 'Article.count', "An Article should not be destroyed" do
# post :create, :article => invalid_attributes # post :create, :article => invalid_attributes
# end # end
def assert_no_difference(expressions, message = nil, &block) def assert_no_difference(expression, message = nil, &block)
assert_difference expressions, 0, message, &block assert_difference expression, 0, message, &block
end end
end end
end end
Expand Down
7 changes: 5 additions & 2 deletions activesupport/test/test_test.rb
Expand Up @@ -66,7 +66,8 @@ def test_array_of_expressions_identify_failure
end end
fail 'should not get to here' fail 'should not get to here'
rescue Exception => e 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 end


def test_array_of_expressions_identify_failure_when_message_provided def test_array_of_expressions_identify_failure_when_message_provided
Expand All @@ -75,7 +76,9 @@ def test_array_of_expressions_identify_failure_when_message_provided
end end
fail 'should not get to here' fail 'should not get to here'
rescue Exception => e 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 end
else else
def default_test; end def default_test; end
Expand Down

0 comments on commit 7564d98

Please sign in to comment.