我想要一些幫助來整理這個場景。我有一個Akka actor,我想要注入一個依賴項,在這個例子中是RemoteFetcher,我也想在我的測試中模擬它。像這樣:Scala隱式類依賴注入
主/ src目錄/斯卡拉/ mypackage中/ Services.scala
package mypackage
import RemoteFetcherFileSystem._
trait RemoteFetcher {
def fetch(path:String): Future[Stream[String]]
}
class MyRemoteResourceActor extends Actor with ActorLogging {
def fetchRemote(path:String) = implicitly[RemoteFetcher].fetch(path)
def receive = {
case FetchRemoteResource(path) => fetchRemote(path).map(_.foreach(sender ! _))
}
}
對於這個工作我有我導入到上述文件的隱式對象。看起來是這樣的:
implicit object RemoteFetcherFileSystem extends RemoteFetcher {
def fetchRemote(path:String) = Future[Stream[String]] { ... reading from file system ... }
}
現在在我的測試中,我有來自akka-testkit的TestActor。在這裏,我想,而不是導入我的模擬依賴:
implicit object RemoteFetcherMock extends RemoteFetcher {
def fetchRemote(path:String) = Future[Stream[String]] { ... mock implementation ... }
}
我的問題是,編譯Services.scala我需要進口隱含對象。但是,我怎麼去在我的測試文件中覆蓋/覆蓋這個。我沒有使用隱式參數的原因是我想避免必須修改我所有的actor構造函數參數。
我環顧四周,閱讀了類型依賴注入模式,並根據教程讓它工作,但當我想在我的示例中進行測試和覆蓋時,我沒有得到它的工作。
所以我想你建議在這裏使用蛋糕模式?那麼值得一試。我在教程中看到它非常詳細,但看起來不錯。 – Magnus
我添加了另一個答案,用implicits來實現。也許不是你想要它,或者最好的方式,但它絕對是一個起點。 –
我想你的意思是讓演員擴展RemoteFetcherFileSystemComponent? – Magnus