Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Scala中伪造此接口?_Scala_Unit Testing_Redis - Fatal编程技术网

如何在Scala中伪造此接口?

如何在Scala中伪造此接口?,scala,unit-testing,redis,Scala,Unit Testing,Redis,我试图在Scala中伪造一个接口。在我的规范中,我希望将fakeHmGetResult分配给测试所需的任何内容 package foo import com.redis.serialization.{Format, Parse} import org.scalatest.FunSpec class SystemUnderTest(fake: ClassToFake) { def redisKey(stationId: Any): String = "pdq" def system

我试图在Scala中伪造一个接口。在我的规范中,我希望将fakeHmGetResult分配给测试所需的任何内容


package foo

import com.redis.serialization.{Format, Parse}
import org.scalatest.FunSpec

class SystemUnderTest(fake: ClassToFake) {
  def redisKey(stationId: Any): String = "pdq"

  def systemUnderTest(stationId: Any): Option[StationInfo] = {
    val a = fake.hmget(redisKey(stationId))

    val b = a.get // fails here

    None
  }

}

class TestClass extends FunSpec {

  val fake: ClassToFake = new ActualFake
  val instance = new SystemUnderTest(fake)

  it("should work at runtime") {
    instance.systemUnderTest("123")

    assert(fake.asInstanceOf[ActualFake].getCount === 1)
  }

}

abstract class ClassToFake {
  def hmget[K, V](key: scala.Any, fields: K*)(implicit format: com.redis.serialization.Format, parseV: com.redis.serialization.Parse[V]): scala.Option[_root_.scala.Predef.Map[K, V]]
}

class ActualFake extends ClassToFake {

  var fakeHmgetResult: Map[Any, String] = Map() // compiler doesn't like this...
  var getCount = 0

  override def hmget[K, V](key: Any, fields: K*)(implicit format: Format, parseV: Parse[V]): Option[Map[K, V]] = {
    getCount = getCount + 1
    Some(fakeHmgetResult) 
  }

}

/* Solution code */

class FakeRedisClient extends RedisClient {

  def reset() = {
    setCount = 0
    getCount = 0
    delCount = 0
    keysCount = 0
    fakeHmgetResult = Map()
    fakeKeys = None
  }

  var setCount = 0
  override def hmset(key: Any, map: Iterable[Product2[Any, Any]])(implicit format: Format): Boolean = {
    setCount = setCount + 1
    true
  }

  var getCount = 0

  var fakeHmgetResult: Map[String, String] = Map()

  override def hmget[K, V](key: Any, fields: K*)(implicit format: Format, parseV: Parse[V]): Option[Map[K, V]] = {
    getCount = getCount + 1
    if (fakeHmgetResult.isEmpty) None
    else Some(fakeHmgetResult.asInstanceOf[Map[K,V]])
  }

  var delCount = 0

  override def del(key: Any, keys: Any*)(implicit format: Format): Option[Long] = {
    delCount = delCount + 1
    None
  }

  var keysCount = 0

  var fakeKeys: Option[List[Option[String]]] = None
  override def keys[A](pattern: Any)(implicit format: Format, parse: Parse[A]): Option[List[Option[A]]] = {
    keysCount = keysCount + 1
    fakeKeys.asInstanceOf[Option[List[Option[A]]]]
  }

}

在这一行

Some(fakeHmgetResult)

您返回
选项[Map[Any,String]]
但在方法签名中您承诺了
选项[Map[K,V]]

查看问题中的最后一个类。。。对我需要的API调用执行此技巧。

请粘贴代码而不是屏幕截图。谢谢!不要忘记实际的错误消息(而不是“有时在堆栈跟踪中使用“$colon$colon”)。什么是无法转换为什么?已编辑。可怜的人nettiquette@jasonnerothin谢谢你,德米特罗。我在原始代码的底部粘贴了工作解决方案。。。Map[String,String]加上hmget()最后一行中的typecast就成功了。