Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplify API of assert_difference by passing in an expression that is…

… evaluated before and after the passed in block. See documenation for examples of new API. [Marcel Molina Jr.]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6693 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 689b529ea82b2375ee3f743eb82da9b83e0d00ff 1 parent c7befb8
Marcel Molina authored
View
2  activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Simplify API of assert_difference by passing in an expression that is evaluated before and after the passed in block. See documenation for examples of new API. [Marcel Molina Jr.]
+
* Added assert_difference and assert_no_difference to test/unit assertions [Tobias Luetke]
* Removed breakpointer and Binding.of_caller in favor of relying on ruby-debug by Kent Sibilev since the breakpointer has been broken since Ruby 1.8.4 and will not be coming back [DHH]
View
51 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
View
47 activesupport/test/test_test.rb
@@ -3,32 +3,47 @@
class AssertDifferenceTest < Test::Unit::TestCase
def setup
- @object = Class.new { attr_accessor :num }.new
+ @object = Class.new do
+ attr_accessor :num
+ def increment
+ self.num += 1
+ end
+
+ def decrement
+ self.num -= 1
+ end
+ end.new
+ @object.num = 0
end
def test_assert_no_difference
- @object.num = 0
-
- assert_no_difference @object, :num do
+ assert_no_difference '@object.num' do
# ...
end
-
end
+
def test_assert_difference
- @object.num = 0
-
-
- assert_difference @object, :num, +1 do
- @object.num = 1
+ assert_difference '@object.num', +1 do
+ @object.increment
end
-
end
- def test_methods_available
-
- assert self.respond_to?(:assert_difference)
- assert self.respond_to?(:assert_no_difference)
-
+ def test_assert_difference_with_implicit_difference
+ assert_difference '@object.num' do
+ @object.increment
+ end
end
+ def test_arbitrary_expression
+ assert_difference '@object.num + 1', +2 do
+ @object.increment
+ @object.increment
+ end
+ end
+
+ def test_negative_differences
+ assert_difference '@object.num', -1 do
+ @object.decrement
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.