【问题标题】:How to intercept exception in Future in scalatest?如何在scalatest中拦截Future中的异常?
【发布时间】: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


【解决方案1】:

已解决,我应该使用全名 -

_ shouldBe userapi.model.UserCreateException(" incorrect name")

【讨论】:

    猜你喜欢
    • 2010-09-18
    • 2016-03-14
    • 1970-01-01
    • 2014-07-19
    • 2013-12-14
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多