diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-09-24 14:19:27 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-09-24 14:19:27 -0300 |
commit | adfb823af52d368fa4d88731a9809a314ad884ad (patch) | |
tree | 2b23c1dd5a71288623faed95d6118bbe2ece9736 | |
parent | 9b966f6233fb9587bea476020dd9e08e50c03198 (diff) | |
parent | 564b162015bebe4d393ee1160f081aae8c4da1d7 (diff) | |
download | rails-adfb823af52d368fa4d88731a9809a314ad884ad.tar.gz rails-adfb823af52d368fa4d88731a9809a314ad884ad.tar.bz2 rails-adfb823af52d368fa4d88731a9809a314ad884ad.zip |
Merge pull request #21754 from lucasmazza/lm-assert-difference
Make `assert_difference` return the result of the yielded block.
-rw-r--r-- | activesupport/CHANGELOG.md | 13 | ||||
-rw-r--r-- | activesupport/lib/active_support/testing/assertions.rb | 4 | ||||
-rw-r--r-- | activesupport/test/test_case_test.rb | 8 |
3 files changed, 23 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 309d842da1..a39344e464 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,14 @@ +* `assert_difference` and `assert_no_difference` now returns the result of the + yielded block. + + Example: + + post = assert_difference -> { Post.count }, 1 do + Post.create + end + + *Lucas Mazza* + * Short-circuit `blank?` on date and time values since they are never blank. Fixes #21657 @@ -12,7 +23,7 @@ * Updated Unicode version to 8.0.0 *Anshul Sharma* - + * `number_to_currency` and `number_with_delimiter` now accept custom `delimiter_pattern` option to handle placement of delimiter, to support currency formats like INR diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb index d87ce3474d..ae8c15d8bf 100644 --- a/activesupport/lib/active_support/testing/assertions.rb +++ b/activesupport/lib/active_support/testing/assertions.rb @@ -68,13 +68,15 @@ module ActiveSupport } before = exps.map(&:call) - yield + retval = yield expressions.zip(exps).each_with_index do |(code, e), i| error = "#{code.inspect} didn't change by #{difference}" error = "#{message}.\n#{error}" if message assert_equal(before[i] + difference, e.call, error) end + + retval end # Assertion that the numeric result of evaluating an expression is not diff --git a/activesupport/test/test_case_test.rb b/activesupport/test/test_case_test.rb index 9e6d1a91d0..18228a2ac5 100644 --- a/activesupport/test/test_case_test.rb +++ b/activesupport/test/test_case_test.rb @@ -56,6 +56,14 @@ class AssertDifferenceTest < ActiveSupport::TestCase end end + def test_assert_difference_retval + incremented = assert_difference '@object.num', +1 do + @object.increment + end + + assert_equal incremented, 1 + end + def test_assert_difference_with_implicit_difference assert_difference '@object.num' do @object.increment |