我试图使用scalamock来模拟Cassandra ScalaGettableData
对象。我需要嘲笑以下方法:
def getMap[K : TypeConverter, V : TypeConverter](name: String) = get[Map[K, V]](name)
TypeConverter
是Trait
,具有隐式实现,例如:
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 :(得分:3)
也许这个例子有帮助:
"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])
明确地模拟出来。