使用specs2进行seq空测试

met*_*ete 6 unit-testing specs scala specs2

如何Seq[String]在Scala中使用specs2 检查a 是否为空?我正在使用seq must be emptyseq.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)
Run Code Online (Sandbox Code Playgroud)

iwe*_*ein 5

我认为类型不匹配错误是由另一部分代码引起的,而不是您发布的代码.

您的示例应该只适用于:

ret must not be empty
Run Code Online (Sandbox Code Playgroud)

我已经尝试并确认工作正常:

 "Seq beEmpty test" should {
    "just work" in {
      Seq("foo") must not be empty
    }
  }
Run Code Online (Sandbox Code Playgroud)

如果每次测试使用多个断言,则可能会遇到麻烦,例如以下内容无法编译:

"Seq beEmpty test" should {
  "just work" in {
    List() must be empty
    Seq("foo") must not be empty
  }
}
Run Code Online (Sandbox Code Playgroud)

这是意料之外的,但通过帮助编译器可以轻松修复:

"Seq beEmpty test" should {
  "just work" in {
    List() must beEmpty
    Seq("foo") must not beEmpty
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 2

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

ret must not have size (0)
Run Code Online (Sandbox Code Playgroud)