【问题标题】:How do you check a mock was called with a Seq irrespective of order无论顺序如何,您如何检查是否使用 Seq 调用了模拟
【发布时间】:2019-10-17 12:25:12
【问题描述】:

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

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

例如:

myMethod(Seq(0,1)) wasCalled once

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

【问题讨论】:

    标签: scala scalatest mockito-scala


    【解决方案1】:

    考虑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)
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-21
      • 1970-01-01
      • 2013-10-12
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      相关资源
      最近更新 更多