我如何检查 Seq[String]
在 Scala 中使用 specs2 是否为空?我正在使用 seq must be empty
或 seq.length must be greaterThan(0)
但我最终总是遇到类型不匹配错误。
ret is Seq[String]
ret.length must be greaterThan(0)
[error] ApiTest.scala:99: type mismatch;
[error] found : Int
[error] required: org.specs2.matcher.Matcher[String]
[error] ret.length must be greaterThan(0)
最佳答案
我认为类型不匹配错误是由不同于您发布的代码的另一位代码引起的。
你的例子应该只适用于:
ret must not be empty
我已经尝试并确认可以正常工作:
"Seq beEmpty test" should {
"just work" in {
Seq("foo") must not be empty
}
}
如果您在每个测试中使用多个断言,您可能会遇到问题,例如以下内容无法编译:
"Seq beEmpty test" should {
"just work" in {
List() must be empty
Seq("foo") must not be empty
}
}
这是出乎意料的,但可以通过帮助编译器轻松解决:
"Seq beEmpty test" should {
"just work" in {
List() must beEmpty
Seq("foo") must not beEmpty
}
}
关于unit-testing - 使用specs2进行序列空测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12511310/