我想测试返回 twitter.Future 的代码是否会在未满足要求时抛出 IllegalArgumentException。
我正在使用 scala 测试和从 twitter.Future 到 FutureConcept 的隐式转换(类似于 ScalaFutures 特性的工作方式,但用于 twitter Futures)
但是我找不到应该怎么做!
我试过以下方法:
whenReady(methodThrowingExceptionFromFuture) {...handling}
这会在到达处理部分之前将异常作为 TestFailedException 抛出,因此它不会根据需要拦截异常。 所以我尝试拦截:
intercept[IllegalArgumentException] { future.futureValue }
但这里也一样。我认为 FutureConcept 将异常包装为 TestFailedException,所以我在考虑解包真正的异常,但肯定有其他方法可以与 scala futures 一起处理负面测试用例?
最佳答案
我在 this response 中找到了答案.基本上,将您对原始 future 的失败
预测传递给whenReady
和亲属。
whenReady(methodThrowingExceptionFromFuture.failed) { ...handling... }
或者只是测试是否抛出了预期的异常类型:
methodThrowingExceptionFromFuture.failed.futureValue shouldBe a [MyException]
关于scala - 测试最大规模的 future 概念是否抛出特定异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28541691/