【问题标题】:Test throwing exception, but ScalaTest/SBT still showing it as a passing test?测试抛出异常,但 ScalaTest/SBT 仍将其显示为通过测试?
【发布时间】:2018-02-09 06:19:35
【问题描述】:

这是有问题的代码块,在我的开发过程中,传递给它的响应在这一点上是无关紧要的,因为实际方法返回了一个不应通过的存根:

responseParser.parseErroredResponse(response)
      .foreach {
        failure => {
          log.debug(s"$failure")
          failure mustBe a[SubmissionFailure]
          failure.message mustEqual "There is a syntax error in one of the queries in the AQuA input"
          failure.code mustEqual "90005"
          failure.names mustEqual Seq("Account", "AccountingPeriod", "NonExistent")
          failure.queries mustEqual Seq(
            "select Id from Account",
            "select Id from AccountingPeriod",
            "select non-existent from non-existent"
          )
        }
      }
  }

目前实际的ResponseParser.parseErroredResponse是这样的:

def parseErroredResponse(response: HttpResponse)
                          (implicit mat: ActorMaterializer,
                           ec: ExecutionContext): Future[SubmissionFailure] = {
    Future(SubmissionFailure("", "", Seq(), Seq(), "", ""))
  }

当我从 IntelliJ 和 SBT 运行测试时,我得到如下结果:

[info] - must parse a failed response and send a `SubmissionFailure` message
[ERROR] [02/08/2018 14:40:19.508] [test-system-akka.actor.default-dispatcher-4] [akka.dispatch.Dispatcher] "[]" did not equal "[There is a syntax error in one of the queries in the AQuA input]"
org.scalatest.exceptions.TestFailedException: "[]" did not equal "[There is a syntax error in one of the queries in the AQuA input]"
    at org.scalatest.MatchersHelper$.indicateFailure(MatchersHelper.scala:340)
    at org.scalatest.MustMatchers$AnyMustWrapper.mustEqual(MustMatchers.scala:6742)
    at hydra.connectors.zuora.AquaActorSpec.$anonfun$new$16(AquaActorSpec.scala:265)
    at scala.util.Success.foreach(Try.scala:249)
    at scala.concurrent.Future.$anonfun$foreach$1$adapted(Future.scala:224)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
    at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
    at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:91)
    at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
    at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:81)
    at akka.dispatch.BatchingExecutor$BlockableBatch.run(BatchingExecutor.scala:91)
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40)
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(ForkJoinExecutorConfigurator.scala:43)
    at akka.dispatch.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at akka.dispatch.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

但它仍然显示为通过测试。我正在使用MustMatchers 特征进行断言,有什么想法可以解决这个测试失败吗?

【问题讨论】:

    标签: scala scalatest


    【解决方案1】:

    测试中的问题是它不管理返回的Future,在这种情况下,.foreach 的主体仅在未来完成时才评估,而测试已经完成。

    要在测试中管理异步计算,您可以从 Scalatest 文档中的此页面 Async testing 开始。

    简而言之,一个解决方案可能是在您的测试类中混入特征org.scalatest.concurrent.ScalaFutures,以便在Future 完成后使用whenReady(<future>)<future>.futureValue 等实用方法检查属性。 一个例子如下:

    whenReady(responseParser.parseErroredResponse(response)) { failure =>
    
      log.debug(s"$failure")
      failure mustBe a[SubmissionFailure]
      failure.message mustEqual "There is a syntax error in one of the queries in the AQuA input"
      failure.code mustEqual "90005"
      failure.names mustEqual Seq("Account", "AccountingPeriod", "NonExistent")
      failure.queries mustEqual Seq(
        "select Id from Account",
        "select Id from AccountingPeriod",
        "select non-existent from non-existent"
      )
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      • 2014-05-10
      • 2020-08-18
      • 2014-07-26
      相关资源
      最近更新 更多