diff options
Diffstat (limited to 'activesupport/lib/active_support/core_ext/test/difference.rb')
-rw-r--r-- | activesupport/lib/active_support/core_ext/test/difference.rb | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/activesupport/lib/active_support/core_ext/test/difference.rb b/activesupport/lib/active_support/core_ext/test/difference.rb index 1f85f0f4b7..65dae3469b 100644 --- a/activesupport/lib/active_support/core_ext/test/difference.rb +++ b/activesupport/lib/active_support/core_ext/test/difference.rb @@ -1,29 +1,40 @@ - module Test #:nodoc: module Unit #:nodoc: - class TestCase #:nodoc: - - # Test difference between the return value of method on object for duration of the block - def assert_difference(objects, method = nil, difference = 1) - objects = [objects].flatten - initial_values = objects.inject([]) { |sum,obj| sum << obj.send(method) } + class TestCase #:nodoc: + # Test numeric difference between the return value of an expression as a result of what is evaluated + # in the yielded block. + # + # assert_difference 'Post.count' do + # post :create, :post => {...} + # end + # + # An arbitrary expression is passed in an evaluated. + # + # assert_difference 'assigns(:post).comments(:reload).size' do + # post :create, :comment => {...} + # end + # + # An arbitrary positive or negative difference can be specified. The default is 1. + # + # assert_difference 'Post.count', -1 do + # post :delete, :id => ... + # end + def assert_difference(expression, difference = 1, &block) + expression_evaluation = lambda { eval(expression) } + original_value = expression_evaluation.call yield - if difference.nil? - objects.each_with_index { |obj,i| - assert_not_equal initial_values[i], obj.send(method), "#{obj}##{method}" - } - else - objects.each_with_index { |obj,i| - assert_equal initial_values[i] + difference, obj.send(method), "#{obj}##{method}" - } - end + assert_equal original_value + difference, expression_evaluation.call end - # Test absence of difference between the return value of method on object for duration of the block - def assert_no_difference(objects, method = nil, &block) - assert_difference objects, method, 0, &block + # Assertion that the numeric result of evaluating an expression is not changed before and after + # invoking the passed in block. + # + # assert_no_difference 'Post.count' do + # post :create, :post => invalid_attributes + # end + def assert_no_difference(expression, &block) + assert_difference expression, 0, &block end - end end end |