-1

I have method:

get[T](method: String, additionalHeaders: (String,String)*)(block: HttpResponse => Try[T]): Future[T]

I try like this:

var serviceClient = mock[ServiceClient]
    (serviceClient .get _).expects(*, *)

But get error:

org.scalamock.function.MockFunction3 cannot be cast to org.scalamock.function.MockFunction2
java.lang.ClassCastException: org.scalamock.function.MockFunction3 cannot be cast to org.scalamock.function.MockFunction2
    at .TestSpec.$anonfun$new$1(TestSpec.scala:22)

I just want to return the same object regardless of the parameters

iluxa1810
  • 278
  • 3
  • 12

1 Answers1

0

In your case you have curried function with three args:

def get[T](method: String, additionalHeaders: (String,String)*)
  (block: HttpResponse => Try[T]): Future[T] = ???

and calling get can be presented as:

Function3[String, Seq[(String, String)], HttpResponse => Try[T], Future[T]]

if we will uncurry get:

trait ServiceWithUncurriedGet {
  def uncurriedGet[T]: (String, Seq[(String, String)], HttpResponse => Try[T]) => Future[T] =
    new Function3[String, Seq[(String, String)], HttpResponse => Try[T], Future[T]] {
      override def apply(v1: String, v2: Seq[(String, String)], v3: HttpResponse => Try[T]): Future[T] =
        serviceClient.get(v1, v2: _*)(v3)
    }
}

and we can use this service to mock uncurriedGet as function with 3 args:

val serviceWithUncurriedGetClient = mock[ServiceWithUncurriedGet]
serviceWithUncurriedGetClient.uncurriedGet[String].expects(*, *, *)

to make your code working you need to make conversion from Curried function to UnCurried and pass types (I use String instead of T type parameter to simplify):

(
  (x: String, y: Seq[(String, String)], f: HttpResponse => Try[String]) => 
    serviceClient.get(x, y: _*)(f)
).expects(*, *, *)

It is also needed expand types of arguments to give compiler more information and it will can convert this into function of 3 args.

Boris Azanov
  • 4,408
  • 1
  • 15
  • 28