加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 如何测试返回未来的方法?

发布时间:2020-12-16 09:13:58 所属栏目:安全 来源:网络整理
导读:我想测试一种返回未来的方法.我的尝试如下: import org.specs2.mutable.Specificationimport scala.concurrent.ExecutionContext.Implicits.globalimport scala.util.{Failure,Success}class AsyncWebClientSpec extends Specification{ "WebClient when do
我想测试一种返回未来的方法.我的尝试如下:

import  org.specs2.mutable.Specification
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure,Success}

class AsyncWebClientSpec extends Specification{

  "WebClient when downloading images" should {
    "for a valid link return non-zero content " in {
      val testImage = AsyncWebClient.get("https://www.google.cz/images/srpr/logo11ww.png")
      testImage.onComplete { res => 
        res match {
          case Success(image) => image must not have length(0)
          case _ =>
        }
        AsyncWebClient.shutDown
      }
    }
  }
}

除了我无法使这个代码工作的事实,我猜可能会有一个更好的方法来测试未来的未来的匹配器的期货.

在specs2中如何正确执行?

解决方法

您可以使用Matcher.await方法将Matcher [T]转换成Matcher [Future [T]]:

val testImage: Future[String] =
   AsyncWebClient.get("https://www.google.cz/images/srpr/logo11ww.png")  

// you must specify size[String] here to help type inference
testImage must not have size[String](0).await

// you can also specify a number of retries and duration between retries
testImage must not have size[String](0).await(retries = 2,timeout = 2.seconds)

// you might also want to check exceptions in case of a failure
testImage must throwAn[Exception].await

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读