【问题标题】:ScalaTest: Checking that list does not contain elementScalaTest:检查列表不包含元素
【发布时间】:2017-10-12 23:30:48
【问题描述】:

所以我是 scala 的新手,我需要检查 List 是否不包含元素。

这是我正在尝试编写的代码,但显然我的语法是错误的,我不知道该怎么做:

  it should "return unique slugs for all category" in {
    val allCategories = categoryService.getAllCategories("rocket")
    val checkedCategories = List()
    allCategories.foreach(
      category => checkedCategories should notContains category.slug
    )
  } 

【问题讨论】:

    标签: scala scalatest


    【解决方案1】:
      checkedCategories should not contain category.slug
    

    您可以使用not contain 进行集合包含检查。

    http://www.scalatest.org/user_guide/using_matchers

    【讨论】:

      【解决方案2】:

      或者,您可以先映射 slug,然后根据其不同的自我检查序列

      val slugs = categoryService.getAllCategories("rocket").map(_.slug)
      slugs.distinct.size shouldBe slugs.size
      

      【讨论】:

        猜你喜欢
        • 2016-09-16
        • 2014-07-31
        • 2022-09-23
        • 2017-11-20
        • 2014-04-30
        • 2016-05-12
        • 1970-01-01
        • 2013-09-17
        • 2018-07-22
        相关资源
        最近更新 更多