1

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

我想检查该方法是否使用具有相同内容的 Seq 调用,但与顺序无关。

例如:

myMethod(Seq(0,1)) wasCalled once

如果我们调用它就会通过myMethod(Seq(1,0))

4

1 回答 1

3

考虑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)
  }
}
于 2019-10-17T12:49:23.023 回答