How to use ScalaTest “contain allOf” on two lists?

前端 未结 3 1184
礼貌的吻别
礼貌的吻别 2021-01-17 13:45

I was looking for a ScalaTest matcher to check that a list contains all of the needed elements (given within another list), but that may also be others.

contai

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-17 14:12

    Another possible solution is:

    import org.scalatest.Inspectors.forAll
    
    forAll(list) { wanted should contain(_) }
    

    Expect an error message similar to this:

    scala> forAll(List(1, 2)) { List(1) should contain(_) }
    org.scalatest.exceptions.TestFailedException: forAll failed, because:
      at index 1, List(1) did not contain element 2 (:18)
    in List(1, 2)
    ...
    Caused by: org.scalatest.exceptions.TestFailedException: List(1) did not contain element 2
    

提交回复
热议问题