【发布时间】:2020-09-11 10:41:19
【问题描述】:
我试图在我的测试中拦截异常(带有异常消息)。
我的测试代码:
class Test4 extends WordSpec with Matchers with ScalaFutures with ScalatestRouteTest {
"should fail if no valid" should {
"should throw ex if name is not valid" in {
whenReady(userService.createUser(wrongNameRequest)) {
_ shouldBe UserCreateException("incorrect name")
}
}
}
但看起来 scalatest 对我的“应该”没有正确反应。它表现为不存在拦截。(我确信:_ shouldBe UserCreateException("incorrect name"))
所以在测试结果中,我得到了这个:
- should should throw ex if a name is not valid *** FAILED ***
[info] The future returned an exception of type: userapi.model.UserCreateException, with the message: incorrect name.
【问题讨论】:
-
我不确定
whenReady是什么,但正如您在 [docs](scalatest.org/user_guide/async 测试) 中看到的那样,您可以使用recoverToExceptionIf[UserCreateException] { FUTUREBLOCK }跨度>
标签: scala playframework akka scalatest