ashic
ashic

Reputation: 6495

Specs2, future, await and ===

I'm trying to figure out if it's possible to use === style matchers for testing futures in Specs2. Consider the following:

f must be_==("def").await f === "def"

If f is a Future[String], the first works, the second fails as (obviously) f is a future and "def" is a string. I can't find a nice way to get the succinctness of === working with futures. I guess I could do Await(f, 1 second) === "def", but that feels icky.

Upvotes: 1

Views: 817

Answers (1)

Eric
Eric

Reputation: 15557

The best you can do is

f.map(_ === "def").await
f.map(_ === "def").await(retries = 1, timeout = 1.seconds)

Otherwise you have to define your own === operator for Futures, something like

implicit class FutureOp[T](f: Future[T]) {
  def ===>(other: T)(implicit retries: Int = 1, 
                     timeout: Duration = 1.second): Result =
  f.map(_ === other).await(retries, timeout)
}

Future("def") ===> "def"

But then you have to rely on implicits to pass retries and timeout.

Upvotes: 1

Related Questions