このシナリオを整理するのに助けが必要です。依存関係を注入したいAkkaアクター、この場合はRemoteFetcherがあります。これも、テストでモックしたいと思います。そのようです:
main / src / scala / 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をコンパイルするには、暗黙のオブジェクトをインポートする必要があるということです。しかし、テストファイルでこれをシャドウ/オーバーライドするにはどうすればよいですか。暗黙の引数を使用しない理由は、すべてのアクターのコンストラクター引数を変更する必要がないようにするためです。
型クラスの依存性注入パターンを見回して読んでいると、チュートリアルに従って機能しますが、例のようにテストしてオーバーライドしたい場合は機能しません。