How to test methods that return Future?

前端 未结 6 2120
时光说笑
时光说笑 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:53

    Await is an anti pattern. Shouldn't ever use it. You can use traits like ScalaFutures, IntegrationPatience, and Eventually.

    Example:

    import org.specs2.mutable.Specification
    import scala.concurrent.ExecutionContext.Implicits.global
    import scala.util.{Failure, Success}
    import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures}
    import scala.concurrent.Future
    
    class AsyncWebClientSpec extends Specification with ScalaFutures
        with IntegrationPatience{
    
        "WebClient when downloading images" should {
            "for a valid link return non-zero content " in {
                whenReady(Future.successful("Done")){ testImage =>
                    testImage must be equalTo "Done"           
                    // Do whatever you need
                }
    
            }
        }
    }
    

提交回复
热议问题