2016-11-28 79 views
1

我试图嘲笑卡桑德拉ScalaGettableData对象使用scalamock。我需要模拟下面的方法:斯卡拉模拟函数与隐含泛型

def getMap[K : TypeConverter, V : TypeConverter](name: String) = get[Map[K, V]](name) 

TypeConverterTrait并具有隐含的实现,例如:

implicit object StringConverter extends TypeConverter[String]

在我的代码我打电话

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] 

我怎么嘲笑这种方法吗?

+0

看看http://stackoverflow.com/a/2983376/411944关于上下文边界如何工作。 – Reactormonk

回答

1

也许这例子有助于:

"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") 
} 

有效地,类型参数K : Bar得到由Scala编译器转换为第二参数列表,其被明确地嘲笑出在该示例中与(_: Bar[Long])

+0

尽管此答案可能会解决问题,但您可以通过解释代码的工作原理或方式来提高答案的质量。 “教一个人钓鱼”的东西。 –

+0

:)有10分。当回复确切文章(问题或答案)作者以外的人时,请确保将它们标记为@Username,甚至还有参与用户的自动完成。 –