Я пытаюсь подделать интерфейс в 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]]]]
}
}