我正在使用带有MiniTest的Ruby 1.9.3。我知道最佳实践是每次测试都有一个断言,但有些情况下我想动态地进行断言,比如比较数组:
if added_skus.each { |s| assert(skus_in_cart.include?(s), "SKUs #{added_skus} or SKU amount do not match SKUs #{skus_in_cart} on Cart page.") }
puts "Product SKUs #{added_skus} match SKUs #{skus_in_cart} on Cart page."
end
如您所知,当它遇到第一个断言失败时,测试结束。我想继续测试并在测试结束后报告所有失败。
我找到了使用旧版test / unit和Ruby 1.8.7(测试/单元库中的add_failure方法)的解决方案,但我找不到MiniTest和Ruby 1.9.3的好解决方案。我真的很感激一些反馈(即使这意味着我做错了 - 请指出我正确的方向)。谢谢!
答案 0 :(得分:2)
解决方案1 - 将断言合并为单个断言:
您可以通过一起考虑所有skus来将当前代码编写为单个断言:
#Determine the missing skus:
missing_skus = added_skus.find_all{ |s| not(skus_in_cart.include?(s)) }
#Assert that there are no missing skus:
assert( missing_skus.empty?, "SKUs #{missing_skus} do not match SKUs on Cart page." )
解决方案2 - 断言多个元素的自定义方法:
如果您确实希望断言计数是每个SKU(而不是整个集合),则可以创建自定义断言方法。虽然我不相信这会比解决方案1带来任何真正的好处。
运行代码示例:
要求'test / unit'
module MiniTest::Assertions
#Asserts that each element passes the test
# Inputs:
# enumerable - The object to iterate over (ex an array)
# msg - Custom message to include in assertion failure
# test - A block that each element will be tested against
def assert_each(enumerable, msg = nil, &test)
msg ||= "No message given."
failed_elements = Array.new
enumerable.each do |e|
self._assertions += 1
failed_elements << e unless yield(e)
end
msg = " #{failed_elements} failed assertion. #{msg}"
raise( MiniTest::Assertion, msg ) unless failed_elements.empty?
end
end
class MyTest < Test::Unit::TestCase
def test()
added_skus = ['b', 'a', 'c', 'd']
skus_in_cart = ['c', 'b']
assert_each(added_skus, "SKUs missing from cart."){ |s| skus_in_cart.include?(s) }
end
end