使用specs2进行序列空测试 [英] Seq empty test with specs2

查看:45
本文介绍了使用specs2进行序列空测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Scala 中使用 specs2 来检查 Seq[String] 是否为空?我正在使用 seq must be emptyseq.length must be GreaterThan(0) 但我最终总是出现类型不匹配错误.

How can I check if a Seq[String] is empty or not using specs2 in Scala ? I am using seq must be empty or seq.length must be greaterThan(0) but I end up always with type mismatch errors.

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)

推荐答案

我认为类型不匹配错误是由不同于您发布的代码的另一位代码引起的.

I think the type mismatch error is caused by another bit of code than that which you've posted.

你的例子应该只适用于:

Your example should just work with:

ret must not be empty

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

I've tried and confirmed to be working correctly:

 "Seq beEmpty test" should {
    "just work" in {
      Seq("foo") must not be empty
    }
  }

如果您在每个测试中使用多个断言,您可能会遇到问题,例如以下内容无法编译:

You could run into trouble if you use multiple assertions per test, for example the following doesn't compile:

"Seq beEmpty test" should {
  "just work" in {
    List() must be empty
    Seq("foo") must not be empty
  }
}

这是出乎意料的,但可以通过帮助编译器轻松解决:

Which is unexpected, but easily fixed by helping the compiler:

"Seq beEmpty test" should {
  "just work" in {
    List() must beEmpty
    Seq("foo") must not beEmpty
  }
}

这篇关于使用specs2进行序列空测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆