断言差异
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了断言差异相关的知识,希望对你有一定的参考价值。
To assert the difference in values before and after some code blocks. Save this as assert_helper.rb and include it in your test_helper.rb
module AssertHelper # Author:: http://blog.caboo.se/articles/2006/06/13/a-better-assert_difference # # == Examples # assert_difference Group, :count do # post :create, :group => { :name => 'monkeys' } # end # # assert_difference [ User, Group ], :count do # Membership.create(:user_id => 1, :group_id => 5) # end # # assert_difference User, :name, nil do # post :update, :id => 5, { :name => 'monkeys' } # end def assert_difference(objects, method = nil, difference = 1) objects = [objects].flatten initial_values = objects.inject([]) { |sum,obj| sum << obj.send(method) } 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 end def assert_no_difference_in_size(object, methods = nil, &block) assert_difference_in_size object, methods, 0, &block end end
以上是关于断言差异的主要内容,如果未能解决你的问题,请参考以下文章