Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

assert_difference can take a callable piece of code rather than just …

…evaling a string
  • Loading branch information...
commit 23eb81a3d1eb154a3aefe569408d1bb2ed63c554 1 parent 1800a6d
@tenderlove tenderlove authored
View
8 activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -576,11 +576,11 @@ def test_reloading_association_with_key_change
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
View
19 activesupport/lib/active_support/testing/assertions.rb
@@ -29,6 +29,16 @@ module Assertions
# 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 Assertions
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

3 comments on commit 23eb81a

@josevalim
Owner

This commit breaks my Rails app. I think it may be related to the fact I define String#call. Check here.

@tenderlove
Owner

I don't always facepunch Jose

@iain

Fuck Reddit, I'm reading only commit notes from now on

Please sign in to comment.
Something went wrong with that request. Please try again.