断言差异

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
  1. module AssertHelper
  2. # Author:: http://blog.caboo.se/articles/2006/06/13/a-better-assert_difference
  3. #
  4. # == Examples
  5. # assert_difference Group, :count do
  6. # post :create, :group => { :name => 'monkeys' }
  7. # end
  8. #
  9. # assert_difference [ User, Group ], :count do
  10. # Membership.create(:user_id => 1, :group_id => 5)
  11. # end
  12. #
  13. # assert_difference User, :name, nil do
  14. # post :update, :id => 5, { :name => 'monkeys' }
  15. # end
  16. def assert_difference(objects, method = nil, difference = 1)
  17. objects = [objects].flatten
  18. initial_values = objects.inject([]) { |sum,obj| sum << obj.send(method) }
  19. yield
  20. if difference.nil?
  21. objects.each_with_index { |obj,i|
  22. assert_not_equal initial_values[i], obj.send(method), "#{obj}##{method}"
  23. }
  24. else
  25. objects.each_with_index { |obj,i|
  26. assert_equal initial_values[i] + difference, obj.send(method), "#{obj}##{method}"
  27. }
  28. end
  29. end
  30.  
  31. def assert_no_difference_in_size(object, methods = nil, &block)
  32. assert_difference_in_size object, methods, 0, &block
  33. end
  34. end

以上是关于断言差异的主要内容,如果未能解决你的问题,请参考以下文章

断言差异

断言差异

2 个 JUnit 断言类之间的差异

ManActivity + 启动时的片段差异

postman测试实例--断言

postman测试实例--断言