代码之家  ›  专栏  ›  技术社区  ›  jasonnerothin

如何在scala中伪造这个接口?

  •  0
  • jasonnerothin  · 技术社区  · 6 年前

    我试图在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]]]]
      }
    
    }
    
    
    0 回复  |  直到 6 年前
        1
  •  1
  •   Dmytro Mitin    6 年前

    在这条线上

    Some(fakeHmgetResult)
    

    你回来 Option[Map[Any, String]] 但在方法签名上你答应过 Option[Map[K, V]] .

        2
  •  0
  •   jasonnerothin    6 年前

    看问题的最后一节课…完成了我需要的api调用。