6

如何Seq[String]在 Scala 中使用 specs2 检查 a 是否为空?我正在使用seq must be emptyorseq.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)
4

2 回答 2

4

我认为类型不匹配错误是由您发布的代码之外的另一位代码引起的。

您的示例应该只适用于:

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
  }
}
于 2013-04-27T18:03:57.353 回答
2

尝试使用 specs2 匹配器have size。由于大小不能为负,如果不为零,则必须大于零。因此,我们可以使用:

ret must not have size (0)
于 2012-11-20T10:12:26.127 回答