2013-08-23 39 views
10

我正在尝试使用期货编写规范,我不知道我应该如何使用Futures特质。我必须通过whenReady a FutureConcept,但我无法找到如何从Future构建一个。文档读取:将期货传递给whenReady失败

为了使whenReady更广泛地应用于,它 接受未来的类型是一个FutureConcept [T],其中T是值的类型由所述将来答应 。将未来传递给whenReady时,需要将您希望传递的未来类型(建模类型) 转换为FutureConcept [T]的隐式 。

,从我明白,我必须写一个FutureFutureConcept之间的隐式转换(这似乎我错了,因为它似乎应该是样板,但它是我可以做它的唯一的事情) 。我无法弄清楚如何做到这一点,虽然,FutureConcept的文档告诉我轻而易举地

见的细节特征期货对文档的语法 这个特质提供了与期货测试。

让我走完整圈。我做了最简单的例子是

import scala.concurrent._ 
import scala.concurrent.ExecutionContext.Implicits.global 

import org.scalatest.WordSpecLike 
import org.scalatest.concurrent._ 

class FutureSpec extends WordSpecLike with Futures { 
    "A future" must { 
    "be a valid argument for whenReady" in { 
     val fut = future { 42 } 
     whenReady(fut) { res => s should be 42 } 
    } 
    } 
} 

,这不符合

  • 类型不匹配编译;发现:scala.concurrent.Future [Int] required:FutureSpec.this.FutureConcept [?]
  • ';'预期但发现整数字面值。

我应该做什么不同?

回答

23

我发现隐式转换存在于ScalaFutures,而不是在Futures。类声明应该是

class FutureSpec extends WordSpecLike with ScalaFutures 

除此之外,还有其他一些错误。 FutureSpec也应该有Matchers混合,并res => s是一个愚蠢的笔误,应该是res => res

+1

这个答案是绝对正确的,只是为了增加一点透明度:改变“与期货”的“与ScalaFutures” –

+0

谢谢,这是否更好? – Martijn

+0

这很完美。你的回答帮了我很多。干杯! –

相关问题