gpt4 book ai didi

testing - then 子句中的 spock 单元测试循环

转载 作者:行者123 更新时间:2023-11-28 19:40:33 25 4
gpt4 key购买 nike

我在 then 子句中进行了循环测试:

result.each {
it.name.contains("foo")
it.entity.subEntity == "bar"
}

for (String obj : result2) {
obj.name.contains("foo")
obj.entity.subEntity == "bar"
}

最近我认识到循环并没有真正经过测试。无论我有 foo 或 bar 还是其他任何东西,测试总是绿色的:)我发现,循环必须以不同的方式进行测试,例如与“每个”?但只是将 'each' 更改为 'every' 会引发异常:

result.every {
it.name.contains("foo")
it.entity.subEntity == "bar"
}

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Spec expression: 1: expecting '}', found '==' @ line 1, column 61.
s("foo") it.entity.rootEntity == "bar" }

我应该如何在测试中正确使用循环?我正在使用 spock 0.7-groovy-2.0

最佳答案

要么使用显式断言语句:

result.each {
assert it.name.contains("foo")
assert it.entity.subEntity == "bar"
}

或者 every 中的单个 bool 表达式:

result.every {
it.name.contains("foo") && it.entity.subEntity == "bar"
}

关于testing - then 子句中的 spock 单元测试循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15224682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com