How to test methods that return Future?

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

    onComplete returns Unit, so that block of code returns immediately and the test ends before being able to do anything. In order to properly test the result of a Future, you need to block until it completes. You can do so using Await, and setting a maximum Duration to wait.

    import scala.concurrent._
    import scala.concurrent.duration._
    
    Await.result(testImage, Duration("10 seconds")) must not have length(0)
    

提交回复
热议问题