lundi 1 août 2016

Testing a unit with implicit class in Scala

Imagine I have a service:

class ServiceA(serviceB: ServiceB) {

   import Extractor._

   def send(typeA: A) = serviceB.send(typeA.extract)

}

object Extractor {
   implicit class Extractor(type: A) {
      def extract = ???
   }
}

I want the extract method to be an implicitly defined because it doesn't directly relate to A type/domain and is a solution specific adhoc extension.

Now I would like to write a very simple unit test that confirms that serviceB.send is called.

For that, I mock service and pass a mocked A to send. Then I could just assert that serviceB.send was called with the mocked A.

As seen in the example, the send method also does some transformation on typeA parameter so I would need to mock extract method to return my specified value. However, A doesn't have extract method - it comes from the implicit class.

So the question is - how do I mock out the implicit class as in the example above as imports are not first class citizens.

Aucun commentaire:

Enregistrer un commentaire