0
votes

I want to get the result of a Future without blocking operations. If I write my code with "await", it works but it is not good for me because it is blocking:

val t: Future[MatchResult[Personne]] = db.getPersonne(userId).map(_.get must beEqualTo(personne))
t.await

I tried to change my code with map:

 val r: Future[MatchResult[Personne]] = db.getPersonne(userId).map(_.get must beEqualTo(personne))
   r.map {
     case r@isWhatIExpected => r
     case isNot => isNot
   }

but I have this error:

found : scala.concurrent.Future[org.specs2.matcher.MatchResult[Personne]]
[error] required: org.specs2.specification.create.InterpolatedFragment

2
When you are in a Future context, you cannot get out of it without waiting. However, you can use onComplete to consume the result when it is ready.marstran
Your question seems to be about spec2 not futures in general. In general you can use fut.foreach or fut.map to continue working with the values of a future.0__

2 Answers

2
votes

Using Specs2 as it appears, there are helpers to test async functions.

import org.specs2.concurrent.{ExecutionEnv => EE}

"Foo" in { implicit ee: EE => // take care of ee
  myAsyncFunWithFuture must beEqualTo(expectedVal).await(timeToWait)
}
0
votes

@user4650183 I think I don't understand your question then. Something somewhere has to wait, or block if you prefer, on the result before using it.