本文介绍了对于返回类型为Future [Unit]的scala方法,断言的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种方法.此方法可能返回Future.failed(.....)或Future.successful(()).

I have a method. This method may return Future.failed(.....) or Future.successful(()).

def计算(x:整数,y:整数):未来[单位] = {........}

def calculate(x: Int, y: Int): Future[Unit] = {........}

现在我需要测试此方法.为验证Future.successful(())大小写的测试断言的最佳方法是什么.

Now I need to test this method. What is the best way to assert for the test which verifies Future.successful(()) case .

推荐答案

Scalatest提供了几种使用Future s的方法.

Scalatest offers a few ways of working with Futures.

选项1: isReadyWithin

import org.scalatest.concurrent.ScalaFutures._
import scala.concurrent.duration._

calculate(1, 3).isReadyWithin(1.second) should be(true)

如果您想在此处做一些具有返回值的操作,则可以使用whenReady:

If you wanted to do something with some return value here, you could use whenReady:

implicit val patienceConfig = PatienceConfig(1.second)

def calculateWithResult(i: Int, j: Int): Future[Int] = ???

whenReady(calculateWithResult(1,3)) { result =>
    result should be(4)
}

您需要在范围内隐式的PatienceConfig,该隐式的PatienceConfig告知whenReady何时由于超时而导致测试失败.我相信scalatest库中有一个默认库,但是选择的时间段很短-大约10毫秒-经常会导致不稳定的测试.

You need an implicit PatienceConfig in scope that tells whenReady when to fail the test because of a timeout. I believe there's a default one in one of the scalatest libraries but the time period chosen is quite short - something on the order of 10 milliseconds - and can often cause flaky tests.

选项2: AsyncXSpec

FlatSpecFreeSpecFunSpec等特征的Async个变种.它们的工作方式与它们的同步变体相同,不同之处在于,现在任何测试都必须返回类型为Future[Assertion]的值.例如:

There are Async varieties of the FlatSpec, FreeSpec, FunSpec, etc. traits. They work much as their synchronous varieties except that any test must now return a value of type Future[Assertion]. For instance:

class Test extends AsyncFreeSpec {
  "An asynchronous method" - {
    "should succeed" in {
      calculate(1,3).map { _ =>
        // just want to make sure the future completes
        succeed
      }
    }
  }
}

同样,您可以在此处对结果进行测试.请注意,此变体意味着测试类中的每个测试必须返回Future,因此如果您要混合使用同步测试和异步测试,就不好了.老实说,我也不确定AsyncXSpec如何选择其超时值.

Again, you can run a test against the result here. Note that this variant means that every test in your test class must return a Future, so it's not great if you want to mix synchronous and asynchronous tests. I'm also honestly not sure how AsyncXSpec chooses its timeout value.

不要选择:Await.result

我建议不要使用Await.result,因为它会在持续时间内阻塞线程.据我所知,以上两个选项都是经过设计的,以便可以轻松地并行运行异步测试.

I would recommend against using Await.result as it blocks the thread for the duration. To my knowledge, the above two options are designed so asynchronous tests can be easily run in parallel.

注意事项:

在进行异步测试时,您要非常小心超时.太长了,如果出了点问题,您的测试可能会持续很长时间.太短了,您的测试将变得不稳定.而且该程序在不同的环境中可能会执行不同的操作,因此您可能会发现,在本地计算机上完全足够的超时会使构建服务器上的测试失败5%.小心!

You want to be super careful with your timeouts when doing asynchronous testing. Too long, and your tests can end up hanging for ages if something goes wrong. Too short, and your tests will be flaky. And the program may perform differently in different environments, so you might find that a timeout that's perfectly sufficient on your local machine has the tests on the build server failing 5% of the time. Be careful!

这篇关于对于返回类型为Future [Unit]的scala方法,断言的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:56