KarelCemus / play-redis

Play framework 2 cache plugin as an adapter to redis-server
Mozilla Public License 2.0
166 stars 44 forks source link

How to Mocking CacheAsyncApi #239

Closed mendiVatbox closed 3 years ago

mendiVatbox commented 3 years ago

I'm using CacheAsyncApi in my Scala class:

class AggregationService @Inject()(@NamedCache("name") cache: CacheAsyncApi){} and I want to do a unit test for it. so tried to do FakeChache with implement the CacheAsyncApi methods.

the problem is that I didn't succeed to implement the 'map' functionality :

the function should be as follow:

override def map[T](key: String)(implicit evidence$8: ClassTag[T]): RedisMap[T, AsynchronousResult] = ??? how to test CacheAsyncApi functionality? And in particular the map functionality

build.sbt includes:

`java 8`
scalaVersion := "2.12.8"
  lazy val playLibs = Seq(
    "com.typesafe.play" %% "play-cache" % "2.7.2",
    "com.github.karelcemus" %% "play-redis" % "2.4.0"
  )

thanks in advance.

KarelCemus commented 3 years ago

I suggest to return mock of RedisMap[T, AsynchronousResult], therefore an instance mocking/implementing all its methods. Have you tried that? Would it work for you? The instance could record calls and return expected values or just use some mocking framework such as scalamock and mock this trait.

mendiVatbox commented 3 years ago

Thank you @KarelCemus for the super quick answer! so this is exactly my question, how to implement it? Do you have any example? how should I return RedisMap[T, AsynchronousResult]?

KarelCemus commented 3 years ago

This project uses a framework to mock traits, look here how the mock is created and here how to use it.

If you want to do it yourself, then there are various ways how to do it, you can google it. You can mock it just as easily as any other trait, there is nothing special about it. Unless you provide more details or your code example, I am unable to help you more. In general, mocks return predefined values and fail on unexpected calls, which both you can easily implement yourself.

However, this issue tracker is not intended for a general discussion on unrelated things, so please either provide specific play-redis related issues or close this ticket.

mendiVatbox commented 3 years ago

@KarelCemus this is exactly what I looking for, thank you very much! appreciate it.