测试抛出异常,但 ScalaTest/SBT 仍然显示为通过测试?
Test throwing exception, but ScalaTest/SBT still showing it as a passing test?
这是有问题的代码块,传递给它的响应在我的开发中此时是无关紧要的,因为实际方法返回了一个不应该传递的存根:
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(AquaActorSpec.scala:265)
at scala.util.Success.foreach(Try.scala:249)
at scala.concurrent.Future.$anonfun$foreach$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(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
特性进行断言,有什么想法可以修复以使此测试失败吗?
测试中的问题是它不管理返回的 Future
,在这种情况下 .foreach
的主体仅在 future 完成后才进行评估,而测试已经完成完成。
要在测试中管理异步计算,您可以从 Scalatest 文档中的此页面 Async testing 开始。
简而言之,一个解决方案可能是在您的测试 class 中混入特征 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"
)
}
这是有问题的代码块,传递给它的响应在我的开发中此时是无关紧要的,因为实际方法返回了一个不应该传递的存根:
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(AquaActorSpec.scala:265)
at scala.util.Success.foreach(Try.scala:249)
at scala.concurrent.Future.$anonfun$foreach$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(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
特性进行断言,有什么想法可以修复以使此测试失败吗?
测试中的问题是它不管理返回的 Future
,在这种情况下 .foreach
的主体仅在 future 完成后才进行评估,而测试已经完成完成。
要在测试中管理异步计算,您可以从 Scalatest 文档中的此页面 Async testing 开始。
简而言之,一个解决方案可能是在您的测试 class 中混入特征 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"
)
}