I was trying to mock my call-by-name function with ScalaMock, so it can run the passed function inside my mock. My workmates helped me to find the solution.
Error: scala.concurrent.impl.Promise$DefaultPromise@69b28a51 cannot be cast to Either
The problem is related with my mock for myFunction(). I pass a call-by-name function to myFunction()(f: => T) which returns T, after evaluating it, myFunction() returns Either[ErrorCode, T]. So the mock should be like that:
class MyTest extends Specification with MockFactory {
trait myTrait {
def myFunction[T](id: Int, name: String)(f: => T): Either[ErrorCode,T]
}
def futureFunction() = Future {
sleep(Random.nextInt(500))
10
}
"Mock my trait" should {
"work" in {
val test = mock[myTrait]
(test.myFunction (_: Int)(_: String)(_: T)).expects(25, "test",*).onCall { test =>
Right(test.productElement(2).asInstanceOf[() => T]())
}
test.myFunction(25)("test")(futureFunction()) must beEqualTo(10)
}
}
}
You can check it from stackoverflow too: stackoverflow