带有隐式泛型类型的Scala模拟函数

带有隐式泛型类型的Scala模拟函数,scala,scalamock,Scala,Scalamock,我试图使用scalamock模拟Cassandra ScalaGettableData对象。我需要模拟以下方法: def getMap[K : TypeConverter, V : TypeConverter](name: String) = get[Map[K, V]](name) TypeConverter是一种特性,具有隐式实现,例如: 隐式对象StringConverter扩展TypeConverter[字符串] 我用我的密码打电话 scalaGettableData.getMap[S

我试图使用scalamock模拟Cassandra ScalaGettableData对象。我需要模拟以下方法:

def getMap[K : TypeConverter, V : TypeConverter](name: String) = get[Map[K, V]](name)
TypeConverter是一种特性,具有隐式实现,例如:

隐式对象StringConverter扩展TypeConverter[字符串]

我用我的密码打电话

scalaGettableData.getMap[String,String]myMap

我猜它隐式地转换为

scalaGettableData.getMap[StringConverter,StringConverter]myMap

我的测试代码如下:

val cassandraRow1 = mock[ScalaGettableData]
(cassandraRow1.getMap[String, String] _).expects("localizations_config").returning(Map("key1" -> "value1"))`
但我得到了编译错误:

Error:(28, 26) _ must follow method; cannot follow (name: String)(implicit evidence$3: com.datastax.spark.connector.types.TypeConverter[String], implicit evidence$4: com.datastax.spark.connector.types.TypeConverter[String])Map[String,String] <and> (index: Int)(implicit evidence$3: com.datastax.spark.connector.types.TypeConverter[String], implicit evidence$4: com.datastax.spark.connector.types.TypeConverter[String])Map[String,String]

我该如何模拟这种方法呢?

也许这个例子有助于:

"implicit parameters" should "be mockable" in {
  trait Bar[T]
  trait Foo {
    def getMap[K : Bar](name: String): Int
  }

  val m = mock[Foo]
  (m.getMap[Long](_: String)(_: Bar[Long])) expects(*, *) returning 42 once()

  implicit val b = new Bar[Long] {}
  m.getMap("bar")
}

实际上,Scala编译器会将类型参数K:Bar转换为第二个参数列表,在本例中使用u:Bar[Long]明确模拟该列表。

看看上下文边界是如何工作的。虽然这个答案可以解决问题,但您可以通过解释代码工作的原因或方式来提高答案的质量。有点像教一个人钓鱼。得了10分。当回复除了发帖人以外的其他人的问题或答案时,请确保将他们标记为@Username,甚至有自动完成的参与用户。