Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing futures to whenReady fails

I'm trying to write a spec for something using futures, and I'm not sure how I'm supposed to use the Futures trait. I have to pass whenReady a FutureConcept, but I can't find how I should construct one from a Future. The documentation reads:

To make whenReady more broadly applicable, the type of future it accepts is a FutureConcept[T], where T is the type of value promised by the future. Passing a future to whenReady requires an implicit conversion from the type of future you wish to pass (the modeled type) to FutureConcept[T].

From that I understand that I have to write an implicit conversion between a Future and a FutureConcept (which seems wrong to me, as it seems that should be boilerplate, but it's the only thing I can make of it). I can't figure out how to do that though, the documentation of FutureConcept tells me handily

See the documentation for trait Futures for the details on the syntax this trait provides for testing with futures.

making me come full circle. The simplest example I cooked up is

import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global

import org.scalatest.WordSpecLike
import org.scalatest.concurrent._

class FutureSpec extends WordSpecLike with Futures {
  "A future" must {
    "be a valid argument for whenReady" in {
      val fut = future { 42 }
      whenReady(fut) { res => s should be 42 }
    }
  }
}

That doesn't compile with

  • type mismatch; found : scala.concurrent.Future[Int] required: FutureSpec.this.FutureConcept[?]
  • ';' expected but integer literal found.

what should I be doing differently?

like image 712
Martijn Avatar asked Aug 23 '13 10:08

Martijn


1 Answers

I found the implicit conversions to be present in ScalaFutures, not in Futures. The class declaration should be

class FutureSpec extends WordSpecLike with ScalaFutures

Other than that, there are a couple of other errors. FutureSpec should also have Matchers mixed in, and res => s is a silly typo, and should be res => res

like image 52
Martijn Avatar answered Sep 23 '22 13:09

Martijn