diff options
author | Tobias Lütke <tobias.luetke@gmail.com> | 2007-06-01 20:20:19 +0000 |
---|---|---|
committer | Tobias Lütke <tobias.luetke@gmail.com> | 2007-06-01 20:20:19 +0000 |
commit | 13058b018861053be8bf5dbec4cafe51b89e173f (patch) | |
tree | 8042495855f9ba31d192eba869307b4624772a9b /activesupport | |
parent | 4d1c87a069d0e13347f28ff9ce4db2427efe18b1 (diff) | |
download | rails-13058b018861053be8bf5dbec4cafe51b89e173f.tar.gz rails-13058b018861053be8bf5dbec4cafe51b89e173f.tar.bz2 rails-13058b018861053be8bf5dbec4cafe51b89e173f.zip |
Enhance assert_difference to accept arrays of strings which are then evaled
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6926 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/test/difference.rb | 21 | ||||
-rw-r--r-- | activesupport/test/test_test.rb | 6 |
2 files changed, 21 insertions, 6 deletions
diff --git a/activesupport/lib/active_support/core_ext/test/difference.rb b/activesupport/lib/active_support/core_ext/test/difference.rb index 17cac8919d..05eab30603 100644 --- a/activesupport/lib/active_support/core_ext/test/difference.rb +++ b/activesupport/lib/active_support/core_ext/test/difference.rb @@ -19,11 +19,20 @@ module Test #:nodoc: # assert_difference 'Article.count', -1 do # post :delete, :id => ... # end - def assert_difference(expression, difference = 1, &block) - expression_evaluation = lambda { eval(expression, block.binding) } - original_value = expression_evaluation.call + # + # An array of expressions can also be passed in and evaluated. + # + # assert_difference [ 'Article.count', 'Post.count' ], +2 do + # post :create, :article => {...} + # end + def assert_difference(expressions, difference = 1, &block) + expression_evaluations = [expressions].flatten.collect{|expression| lambda { eval(expression, block.binding) } } + + original_values = expression_evaluations.inject([]) { |memo, expression| memo << expression.call } yield - assert_equal original_value + difference, expression_evaluation.call + expression_evaluations.each_with_index do |expression, i| + assert_equal original_values[i] + difference, expression.call + end end # Assertion that the numeric result of evaluating an expression is not changed before and after @@ -32,8 +41,8 @@ module Test #:nodoc: # assert_no_difference 'Article.count' do # post :create, :article => invalid_attributes # end - def assert_no_difference(expression, &block) - assert_difference expression, 0, &block + def assert_no_difference(expressions, &block) + assert_difference expressions, 0, &block end end end diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index b1d916d5c0..944b98f529 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -53,4 +53,10 @@ class AssertDifferenceTest < Test::Unit::TestCase assert_difference('local_scope; @object.num') { @object.increment } end end + + def test_array_of_expressions + assert_difference [ '@object.num', '@object.num + 1' ], +1 do + @object.increment + end + end end |