mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
assert_difference can take a callable piece of code rather than just evaling a string
This commit is contained in:
@@ -576,11 +576,11 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
|
||||
end
|
||||
|
||||
def test_polymorphic_counter_cache
|
||||
tagging = taggings(:welcome_general)
|
||||
post = post = posts(:welcome)
|
||||
comment = comments(:greetings)
|
||||
tagging = taggings(:welcome_general)
|
||||
post = posts(:welcome)
|
||||
comment = comments(:greetings)
|
||||
|
||||
assert_difference 'post.reload.taggings_count', -1 do
|
||||
assert_difference lambda { post.reload.taggings_count }, -1 do
|
||||
assert_difference 'comment.reload.taggings_count', +1 do
|
||||
tagging.taggable = comment
|
||||
end
|
||||
|
||||
@@ -29,6 +29,16 @@ module ActiveSupport
|
||||
# post :create, :article => {...}
|
||||
# end
|
||||
#
|
||||
# A lambda or a list of lambdas can be passed in and evaluated:
|
||||
#
|
||||
# assert_difference lambda { Article.count }, 2 do
|
||||
# post :create, :article => {...}
|
||||
# end
|
||||
#
|
||||
# assert_difference [->{ Article.count }, ->{ Post.count }], 2 do
|
||||
# post :create, :article => {...}
|
||||
# end
|
||||
#
|
||||
# A error message can be specified.
|
||||
#
|
||||
# assert_difference 'Article.count', -1, "An Article should be destroyed" do
|
||||
@@ -37,14 +47,15 @@ module ActiveSupport
|
||||
def assert_difference(expression, difference = 1, message = nil, &block)
|
||||
b = block.send(:binding)
|
||||
exps = Array.wrap(expression)
|
||||
before = exps.map { |e| eval(e, b) }
|
||||
before = exps.map { |e| e.respond_to?(:call) ? e.call : eval(e, b) }
|
||||
|
||||
yield
|
||||
|
||||
exps.each_with_index do |e, i|
|
||||
error = "#{e.inspect} didn't change by #{difference}"
|
||||
error = "#{message}.\n#{error}" if message
|
||||
assert_equal(before[i] + difference, eval(e, b), error)
|
||||
error = "#{e.inspect} didn't change by #{difference}"
|
||||
error = "#{message}.\n#{error}" if message
|
||||
actual = e.respond_to?(:call) ? e.call : eval(e, b)
|
||||
assert_equal(before[i] + difference, actual, error)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user