有没有一种方法在布尔型的ZIO测试中添加一个描述性的断言消息?

huangapple go评论55阅读模式
英文:

Is there a way to add a descriptive Assert message in a Boolean ZIO Test

问题

我有一些要测试的布尔值,如下:

assert(g8Exists, equalTo(true)) &&
assert(projectExists, equalTo(true)) &&
assert(testenvExists, equalTo(true)) ...

如果其中一个失败,我只会得到:

false did not satisfy equalTo(true)

不知道哪一行失败了。有没有办法我可以添加一个描述性的断言消息。例如:

assert(g8Exists, equalTo(true), "g8Exists")

或者更好的方式:

assertTrue(g8Exists, "g8Exists")

会得到:

false did not satisfy equalTo(true) - g8Exists

或者一般情况下有更好的测试布尔值的方法吗?

英文:

I have a couple of Booleans I want to test, like

assert(g8Exists, equalTo(true)) &&
assert(projectExists, equalTo(true)) &&
assert(testenvExists, equalTo(true)) ...

If one fails, all I get is:

false did not satisfy equalTo(true)

No clue which line failed. Is there a way I can add a descriptive Assert message. For example:

assert(g8Exists, equalTo(true), "g8Exists")

Or preferred:

assertTrue(g8Exists, "g8Exists")

Would result in

false did not satisfy equalTo(true) - g8Exists

Or is there a better way to test Booleans in general?

答案1

得分: 6

是的!您可以在Assertion或其符号别名??上使用label方法。

assert(g8Exists, isTrue ?? "g8Exists") &&
assert(projectExists, isTrue ?? "projectExists") &&
assert(testenvExists, isTrue ?? "testenvExists")

假设第一个断言失败,您将获得一个清晰的错误消息,指示失败的断言是哪一个。

false did not satisfy isTrue()
false did not satisfy (isTrue() ?? "g8Exists")
英文:

Yes! You can use the label method on Assertion or its symbolic alias ?? for this.

assert(g8Exists, isTrue ?? "g8Exists") &&
assert(projectExists, isTrue ?? "projectExists") &&
assert(testenvExists, isTrue ?? "testenvExists")

Assuming that the first assertion fails you would get a nice error message indicating exactly which assertion failed.

false did not satisfy isTrue()
false did not satisfy (isTrue() ?? "g8Exists")

huangapple
  • 本文由 发表于 2020年1月3日 18:05:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定