Ticket #6611: assert_difference.patch
| File assert_difference.patch, 4.7 kB (added by brandon, 2 years ago) |
|---|
-
actionpack/test/controller/assert_difference_test.rb
old new 1 require File.dirname(__FILE__) + '/../abstract_unit' 2 require File.dirname(__FILE__) + '/fake_controllers' 3 4 class AssertDifferenceTest < Test::Unit::TestCase 5 AssertionFailedError = Test::Unit::AssertionFailedError 6 7 class Model 8 cattr_accessor :count 9 self.count = 4 10 11 attr_accessor :name 12 end 13 14 def setup 15 @model = Model.new 16 end 17 18 def test_default_increment_cattr_by_one 19 assert_nothing_raised { assert_difference(Model, :count) { Model.count += 1 } } 20 end 21 22 def test_increment_cattr_by_one 23 assert_nothing_raised { assert_difference(Model, :count, 1) { Model.count += 1 } } 24 end 25 26 def test_decrement_cattr_by_one 27 assert_nothing_raised { assert_difference(Model, :count, -1) { Model.count -= 1 } } 28 end 29 30 def test_increment_cattr_by_two 31 assert_nothing_raised { assert_difference(Model, :count, 2) { Model.count += 2 } } 32 end 33 34 def test_nil_difference 35 assert_nothing_raised { assert_difference(Model, :count, nil) { Model.count += 2 } } 36 end 37 38 def test_change_attr 39 assert_nothing_raised { assert_difference(@model, :name) { @model.name = 'changed' } } 40 end 41 42 def test_no_difference_without_change 43 assert_nothing_raised { assert_no_difference(@model, :name) { } } 44 end 45 46 def test_fails_no_difference_with_change 47 assert_raises(AssertionFailedError) { assert_no_difference(@model, :name) { @model.name = 'changed' } } 48 end 49 50 def test_fails_off_by_one 51 assert_raises(AssertionFailedError) { assert_difference(Model, :count, 1) { Model.count += 2 } } 52 end 53 54 def test_fails_no_difference_off_by_one 55 assert_raises(AssertionFailedError) { assert_no_difference(Model, :count) { Model.count += 1 } } 56 end 57 58 def test_multiple_objects 59 @model2 = Model.new 60 assert_nothing_raised { assert_difference([@model, @model2], :name) { @model.name = @model2.name = 'changed'} } 61 end 62 63 def test_no_difference_multiple_objects 64 @model2 = Model.new 65 assert_nothing_raised { assert_no_difference([@model, @model2], :name) { } } 66 end 67 end -
actionpack/lib/action_controller/assertions/model_assertions.rb
old new 7 7 assert record.valid?, record.errors.full_messages.join("\n") 8 8 end 9 9 end 10 11 # Asserts that the given block modifies the specified object or objects. The default behavior 12 # is to test that the method called is different by 1 for Fixnums and not equal for any other class. 13 # 14 # assert_difference Group, :count do 15 # post :create, :group => { :name => 'monkeys' } 16 # end 17 # 18 # You can also check that multiple objects have been modified 19 # 20 # assert_difference [ User, Group ], :count do 21 # post :create, :user => { :name => "Curious George" }, :group => { :name => 'monkeys' } 22 # end 23 # 24 # To test blocks that modify the result by more than 1 you can pass a third parameter to indicate 25 # the difference. If you don't care about the difference you can set the third parameter to nil 26 # and it will simply assert that the value has changed. 27 # 28 # This implementation is based off of http://blog.caboo.se/articles/2006/06/13/a-better-assert_difference 29 def assert_difference(objects, method = nil, difference = 1) 30 objects = [objects].flatten 31 initial_values = objects.inject([]) { |sum,obj| sum << obj.send(method) } 32 yield 33 clean_backtrace do 34 objects.each_with_index do |obj,i| 35 if initial_values[i].is_a?(Fixnum) && !difference.nil? 36 assert_equal initial_values[i] + difference, obj.send(method), "#{obj}##{method}" 37 elsif difference == 0 38 assert_equal initial_values[i], obj.send(method), "#{obj}##{method}" 39 else 40 assert_not_equal initial_values[i], obj.send(method), "#{obj}##{method}" 41 end 42 end 43 end 44 end 45 46 # Asserts that the given block does not modify the specified object 47 def assert_no_difference(object, method, &block) 48 assert_difference object, method, 0, &block 49 end 10 50 end 11 51 end 12 52 end