scalamock:如何用重复的参数模拟咖喱方法

时间:2017-01-21 12:25:42

标签: scalamock

我想嘲笑awscala.dynamodbv2.DynamoDB.putConditionalMethod 如何定义一个curried方法的期望值并包含一个重复的参数:

putConditional(tableName: String, attributes: (String, Any)*)(cond: Seq[(String, aws.model.ExpectedAttributeValue)]): Unit

以下是我的工作:

(mockClient.putConditional(_: String, _: (String, Any))(_: Seq[(String, ExpectedAttributeValue)]))
    .expects("Data-Identity-Partitions", 
      *,
      Seq(
        "DatacenterId" -> exp.isNull,
        "InstanceId" -> exp.isNull,
        "TTL" -> exp.isNull
      ))

但是这个:

(mockClient.putConditional(_: String, _: (String, Any))(_: Seq[(String, ExpectedAttributeValue)]))
    .expects("Data-Identity-Partitions",
      Seq("DatacenterId" -> 1,
        "InstanceId" -> 0,
        "TTL" -> System.currentTimeMillis()),
      Seq(
        "DatacenterId" -> exp.isNull,
        "InstanceId" -> exp.isNull,
        "TTL" -> exp.isNull
      ))

导致以下编译器错误:

[error] AwsPartitionActorSpec.scala:76: type mismatch;
[error]  found   : Seq[(String, Any)]
[error]  required: org.scalamock.matchers.MockParameter[(String, Any)]
[error]           Seq[(String, Any)]("DatacenterId" -> 1,
[error]                             ^

1 个答案:

答案 0 :(得分:0)

迟到总比我想的要好,这是我的建议:

  trait testtrait {
    def foo(t: String, a: (String, Any) *): Int
  }

  "foo" should "work" in {
    val m = mock[testtrait]

    m.foo _ expects where {
      case ("foo", Seq(("bar", 42L), ("baz", "mango"))) => true
      case _ => false
    } returns 5


    m.foo("foo", ("bar", 42L), ("baz", "mango")) should be (5)
  }