diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-05-01 11:55:13 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-05-01 11:55:13 -0700 |
commit | 23eb81a3d1eb154a3aefe569408d1bb2ed63c554 (patch) | |
tree | 070d62d2efab0b3af9f365676bed3538c3714f5b /activesupport | |
parent | 1800a6d1c8f400ce3ba5203489090d66025b9e4a (diff) | |
download | rails-23eb81a3d1eb154a3aefe569408d1bb2ed63c554.tar.gz rails-23eb81a3d1eb154a3aefe569408d1bb2ed63c554.tar.bz2 rails-23eb81a3d1eb154a3aefe569408d1bb2ed63c554.zip |
assert_difference can take a callable piece of code rather than just evaling a string
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/testing/assertions.rb | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb index a4e0361a32..bfc2024529 100644 --- a/activesupport/lib/active_support/testing/assertions.rb +++ b/activesupport/lib/active_support/testing/assertions.rb @@ -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 |