测试抛出异常,但ScalaTest / SBT仍显示为通过测试?

时间:2018-02-08 21:49:00

标签: scala scalatest

以下是相关代码块,由于实际方法返回不应传递的存根,因此传递给它的响应在我的开发中此时无关紧要:

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特征进行断言,我可以解决哪些问题让这个测试失败?

1 个答案:

答案 0 :(得分:2)

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

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

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

<future>.futureValue