[英]Scalatest ExecutionContext
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. 我想知道我应该在
scalatest % 2.2.6
上使用哪个ExecutionContext
(以及为什么)运行我的期货和模拟的期货。
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")
}
}
Just import scala.concurrent.ExecutionContext.Implicits.global
and this will load the default ExecutionContext
for the Future
objects in your tests to work properly. 只需导入
scala.concurrent.ExecutionContext.Implicits.global
,这将为测试中的Future
对象加载默认的ExecutionContext
,以使其正常工作。
NOTE: For using Futures on tests is OK the global implicits. 注意:对于在测试中使用期货,可以使用全局隐式。 For real projects consider to use
ExecutionContext
per case. 对于实际项目,请考虑在每种情况下使用
ExecutionContext
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.