How to test methods that return Future?

前端 未结 6 2119
时光说笑
时光说笑 2021-02-07 08:25

I\'d like to test a method that returns a Future. My attempts were as follows:

import  org.specs2.mutable.Specification
import scala.concurrent.Exec         


        
6条回答
  •  北荒
    北荒 (楼主)
    2021-02-07 08:45

    You can use the Matcher.await method to transform a Matcher[T] into a 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
    

提交回复
热议问题