加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

单元测试 – 使用specs2进行Seq空测试

发布时间:2020-12-16 18:55:11 所属栏目:安全 来源:网络整理
导读:如何在 Scala中使用specs2检查Seq [String]是否为空?我使用seq必须为空或seq.length必须大于(0)但我最终总是出现类型不匹配错误. ret is Seq[String]ret.length must be greaterThan(0)[error] ApiTest.scala:99: type mismatch;[error] found : Int[error]
如何在 Scala中使用specs2检查Seq [String]是否为空?我使用seq必须为空或seq.length必须大于(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
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读