10

I would like to know which ExecutionContext I should use (and why) on scalatest % 2.2.6 to run my futures and mock's futures.

class Foo {
   def foo: Future[String] = Future.sucessful("B")
}

class Bar(foo: Foo) {
   def bar: Future[String] = foo.foo()
}

class MyTest extends WordSpec {

  implicit val ec: ExecutionContext = ??? // ...global? Why global? Why not?

  val myMock = mock[Foo]
  val myBar = new Bar(myMock)

  "..." in {
    (myMock.foo _).expects(*).returning(Future.succesful("A"))
    whenReady(myBar.bar())(_ shouldBe "A")
  }
}
hasumedic
  • 2,139
  • 12
  • 17
Henrique Goulart
  • 1,815
  • 2
  • 22
  • 32

1 Answers1

7

Just import scala.concurrent.ExecutionContext.Implicits.global and this will load the default ExecutionContext for the Future objects in your tests to work properly.

NOTE: For using Futures on tests is OK the global implicits. For real projects consider to use ExecutionContext per case.

Patryk Rudnicki
  • 755
  • 1
  • 8
  • 21
Carlos Saltos
  • 1,385
  • 15
  • 15