aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/testing
diff options
context:
space:
mode:
authorJulien Meichelbeck <julien.meichelbeck@gmail.com>2018-01-18 21:20:34 +0100
committerRafael França <rafaelmfranca@gmail.com>2018-01-18 15:20:34 -0500
commite0f0d717d63b7896186f40c4d389977ddba72a97 (patch)
treeb972eb23e30388aa087cddae7559252b03647a14 /activesupport/lib/active_support/testing
parentccfc2d63cade1d68de98c8051fd63c2348c6c914 (diff)
downloadrails-e0f0d717d63b7896186f40c4d389977ddba72a97.tar.gz
rails-e0f0d717d63b7896186f40c4d389977ddba72a97.tar.bz2
rails-e0f0d717d63b7896186f40c4d389977ddba72a97.zip
Support hash as first argument in `assert_difference`. (#31600)
* Support hash as first argument for `assert_difference`. This allows to specify multiple numeric differences in the same assertion. Example: assert_difference 'Article.count' => 1, 'Notification.count' => 2 do # post :create, params: { article: {...} } end * Support error message when passing a hash as a first parameter * Format CHANGELOG properly [Julien Meichelbeck + Rafael Mendonça França]
Diffstat (limited to 'activesupport/lib/active_support/testing')
-rw-r--r--activesupport/lib/active_support/testing/assertions.rb28
1 files changed, 21 insertions, 7 deletions
diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb
index 6f69c48674..a891ff616d 100644
--- a/activesupport/lib/active_support/testing/assertions.rb
+++ b/activesupport/lib/active_support/testing/assertions.rb
@@ -58,6 +58,12 @@ module ActiveSupport
# post :create, params: { article: {...} }
# end
#
+ # A hash of expressions/numeric differences can also be passed in and evaluated.
+ #
+ # assert_difference ->{ Article.count } => 1, ->{ Notification.count } => 2 do
+ # post :create, params: { article: {...} }
+ # end
+ #
# A lambda or a list of lambdas can be passed in and evaluated:
#
# assert_difference ->{ Article.count }, 2 do
@@ -73,20 +79,28 @@ module ActiveSupport
# assert_difference 'Article.count', -1, 'An Article should be destroyed' do
# post :delete, params: { id: ... }
# end
- def assert_difference(expression, difference = 1, message = nil, &block)
- expressions = Array(expression)
-
- exps = expressions.map { |e|
+ def assert_difference(expression, *args, &block)
+ expressions =
+ if expression.is_a?(Hash)
+ message = args[0]
+ expression
+ else
+ difference = args[0] || 1
+ message = args[1]
+ Hash[Array(expression).map { |e| [e, difference] }]
+ end
+
+ exps = expressions.keys.map { |e|
e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
}
before = exps.map(&:call)
retval = yield
- expressions.zip(exps).each_with_index do |(code, e), i|
- error = "#{code.inspect} didn't change by #{difference}"
+ expressions.zip(exps, before) do |(code, diff), exp, before_value|
+ error = "#{code.inspect} didn't change by #{diff}"
error = "#{message}.\n#{error}" if message
- assert_equal(before[i] + difference, e.call, error)
+ assert_equal(before_value + diff, exp.call, error)
end
retval