解决方案 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 增加了任何真正的好处。
运行代码示例:
需要“测试/单元”
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