当使用Specs2(v 2.3.13)等待时,我注意到在某些情况下它不会失败。具体来说,我观察到如果“await”在Scope内部超时,那么它就不会失败。
"Awaiting a failed future should fail" in {
Future.failed(throw new Exception()).map(_ => success).await // Fails Correctly
}
"Awaiting a timed out future should fail" in {
Future(Thread.sleep(5000)).map(_ => success).await // Fails Correctly
}
"Awaiting a failed scope should fail" in new Scope {
failure // Fails Correctly
}
"Awaiting a failed future in a scope should fail" in new Scope {
Future.failed(throw new Exception()).map(_ => success).await // Fails Correctly
}
"Awaiting a timed out future in a scope should fail" in new Scope {
Future(Thread.sleep(5000)).map(_ => success).await // DOES NOT FAIL
}
我误解了用法,还是这个错误?
答案 0 :(得分:0)
.await
隐含的Future[T]
只会创建Result
,如果超时,则会失败。但是不会抛出任何异常,因此结果不会逃脱Scope
特征。
要解决此问题,您需要使用Matchers
并在T
中指定您对Future[T]
类型值的期望:
Future(Thread.sleep(5000)).map(_ => 1) must be_==(1).await
这将正确显示
[info] x Awaiting a timed out future in a scope should fail
[error] Timeout after 1 second (TestSpec.scala:35)