你如何检查模拟是用 Seq 调用的,而不考虑顺序

How do you check a mock was called with a Seq irrespective of order

我有一个方法,已被模拟,并以一个 Seq 作为参数。

我想检查该方法是用具有相同内容的 Seq 调用的,但不考虑顺序。

例如:

myMethod(Seq(0,1)) wasCalled once

如果我们调用 myMethod(Seq(1,0))

就会通过

考虑 argThat 匹配器,它可以指定谓词匹配器

argThat((s: Seq[Int]) => s.sorted == Seq(0,1))

例如

import org.scalatest.{FlatSpec, Matchers}
import org.mockito.{ArgumentMatchersSugar, IdiomaticMockito}

trait Qux {
  def foo(s: Seq[Int]): Int
}

class ArgThatSpec extends FlatSpec with Matchers with IdiomaticMockito with ArgumentMatchersSugar {
  "ArgThat" should "match on a predicate" in {
    val qux = mock[Qux]
    qux.foo(argThat((s: Seq[Int]) => s.sorted == Seq(0,1))) answers (42)
    qux.foo((Seq(1,0))) shouldBe (42)
  }
}