Skip to content

Commit

Permalink
make assert_difference error message not suck
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove authored and fxn committed Aug 13, 2011
1 parent bb72183 commit eaee18d
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions activesupport/lib/active_support/testing/assertions.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ module Assertions
# end # end
def assert_difference(expression, difference = 1, message = nil, &block) def assert_difference(expression, difference = 1, message = nil, &block)
exps = Array.wrap(expression).map { |e| exps = Array.wrap(expression).map { |e|
e.respond_to?(:call) ? e : lambda { eval(e, block.binding) } callee = e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
[e, callee]
} }
before = exps.map { |e| e.call } before = exps.map { |_, block| block.call }


yield yield


exps.each_with_index do |e, i| exps.each_with_index do |(code, block), i|
error = "#{e.inspect} didn't change by #{difference}" error = "#{code.inspect} didn't change by #{difference}"
error = "#{message}.\n#{error}" if message error = "#{message}.\n#{error}" if message
assert_equal(before[i] + difference, e.call, error) assert_equal(before[i] + difference, block.call, error)
end end
end end


Expand Down

0 comments on commit eaee18d

Please sign in to comment.