Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6693

Show
Ignore:
Timestamp:
05/08/07 03:54:34 (2 years ago)
Author:
marcel
Message:

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.]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r6647 r6693  
    11*SVN* 
     2 
     3* 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.] 
    24 
    35* Added assert_difference and assert_no_difference to test/unit assertions [Tobias Luetke] 
  • trunk/activesupport/lib/active_support/core_ext/test/difference.rb

    r6647 r6693  
    1  
    21module Test #:nodoc: 
    32  module Unit #:nodoc: 
    4     class TestCase #:nodoc: 
    5        
    6       # Test difference between the return value of method on object for duration of the block 
    7       def assert_difference(objects, method = nil, difference = 1) 
    8         objects = [objects].flatten 
    9         initial_values = objects.inject([]) { |sum,obj| sum << obj.send(method) } 
     3    class TestCase #:nodoc:       
     4      # Test numeric difference between the return value of an expression as a result of what is evaluated 
     5      # in the yielded block. 
     6      # 
     7      #   assert_difference 'Post.count' do 
     8      #     post :create, :post => {...} 
     9      #   end 
     10      # 
     11      # An arbitrary expression is passed in an evaluated. 
     12      # 
     13      #   assert_difference 'assigns(:post).comments(:reload).size' do 
     14      #     post :create, :comment => {...} 
     15      #   end 
     16      # 
     17      # An arbitrary positive or negative difference can be specified. The default is 1. 
     18      # 
     19      #   assert_difference 'Post.count', -1 do 
     20      #     post :delete, :id => ... 
     21      #   end 
     22      def assert_difference(expression, difference = 1, &block) 
     23        expression_evaluation = lambda { eval(expression) } 
     24        original_value        = expression_evaluation.call 
    1025        yield 
    11         if difference.nil? 
    12           objects.each_with_index { |obj,i| 
    13             assert_not_equal initial_values[i], obj.send(method), "#{obj}##{method}" 
    14           } 
    15         else 
    16           objects.each_with_index { |obj,i| 
    17             assert_equal initial_values[i] + difference, obj.send(method), "#{obj}##{method}" 
    18           } 
    19         end 
     26        assert_equal original_value + difference, expression_evaluation.call 
    2027      end 
    2128 
    22       # Test absence of difference between the return value of method on object for duration of the block 
    23       def assert_no_difference(objects, method = nil, &block) 
    24         assert_difference objects, method, 0, &block 
     29      # Assertion that the numeric result of evaluating an expression is not changed before and after 
     30      # invoking the passed in block. 
     31      # 
     32      #   assert_no_difference 'Post.count' do 
     33      #     post :create, :post => invalid_attributes 
     34      #   end 
     35      def assert_no_difference(expression, &block) 
     36        assert_difference expression, 0, &block 
    2537      end 
    26  
    2738    end 
    2839  end 
  • trunk/activesupport/test/test_test.rb

    r6647 r6693  
    44   
    55  def setup 
    6     @object = Class.new { attr_accessor :num }.new     
     6    @object = Class.new do 
     7      attr_accessor :num  
     8      def increment 
     9        self.num += 1 
     10      end 
     11 
     12      def decrement 
     13        self.num -= 1 
     14      end 
     15    end.new     
     16    @object.num = 0 
    717  end 
    818   
    919  def test_assert_no_difference 
    10     @object.num = 0 
    11      
    12     assert_no_difference @object, :num do 
     20    assert_no_difference '@object.num' do 
    1321      # ... 
    1422    end 
    15        
    16   end 
    17   def test_assert_difference 
    18     @object.num = 0 
    19      
    20      
    21     assert_difference @object, :num, +1 do 
    22       @object.num = 1 
    23     end 
    24        
    2523  end 
    2624 
    27   def test_methods_available 
    28      
    29     assert self.respond_to?(:assert_difference) 
    30     assert self.respond_to?(:assert_no_difference) 
    31      
     25  def test_assert_difference 
     26    assert_difference '@object.num', +1 do 
     27      @object.increment 
     28    end 
    3229  end 
    3330 
     31  def test_assert_difference_with_implicit_difference 
     32    assert_difference '@object.num' do 
     33      @object.increment 
     34    end 
     35  end 
     36 
     37  def test_arbitrary_expression 
     38    assert_difference '@object.num + 1', +2 do 
     39      @object.increment 
     40      @object.increment 
     41    end 
     42  end 
     43 
     44  def test_negative_differences 
     45    assert_difference '@object.num', -1 do 
     46      @object.decrement 
     47    end 
     48  end 
    3449end