scala-redis-nb icon indicating copy to clipboard operation
scala-redis-nb copied to clipboard

Creating a mock RedisClient

Open patrickcunningham opened this issue 9 years ago • 6 comments

Hi, This library is great and hoping to put it into full use shortly. However I am using behind a Factory pattern to abstract away the cache implementation (so we can easily swap in out). I was looking at testing without having a local version of redis running. Testing this is difficult and I would like to create a MockRedisClient that I can use in my factory, maybe that would look something like this:

class MockRedisClient(clientRef: ActorRef) extends RedisClient(clientRef) {
  val mmap = collection.mutable.Map[String, Any]()

  override def get[A](key: String)(implicit timeout: Timeout, reader: Reader[A]): Future[Option[A]] = {
    val value = mmap.get(key)
    Future.successful(value.asInstanceOf[Option[A]])
  }


  override def set(key: String, value: Stringified, exORpx: Option[SetExpiryOption], nxORxx: Option[SetConditionOption])(implicit timeout: Timeout): Future[Boolean] = {
    mmap.put(key, value).
    Future.successful(true)
  }


  override def setex(key: String, expiry: Int, value: Stringified)(implicit timeout: Timeout): Future[Boolean] = {
    mmap.put(key, value)
    Future.successful(true)
  }
}

This is not possible on the get method due to the fact that com.redis.serialization.Reader is private to package com.redis. I was wondering if this was an intended restriction and if this visibility is something you would consider removing so that we could Mock the RedisClient easier? I've tried it on a local copy and is then accessible to to override.

An alternative is to create a local com.redis package and create a MockRedisClient class which is a hacky

patrickcunningham avatar Feb 24 '15 10:02 patrickcunningham