暗黙のパラメーターが関係している場合に、Scala でテストを作成する方法を理解するのに少し苦労しています。
私は自分のコードとテストの次の(短いバージョン)を持っています:
実装 (Scala 2.10、Spray、Akka):
import spray.httpx.SprayJsonSupport._
import com.acme.ResultJsonFormat._
case class PerRequestIndexingActor(ctx: RequestContext) extends Actor with ActorLogging {
def receive = LoggingReceive {
case AddToIndexRequestCompleted(result) =>
ctx.complete(result)
context.stop(self)
}
}
object ResultJsonFormat extends DefaultJsonProtocol {
implicit val resultFormat = jsonFormat2(Result)
}
case class Result(code: Int, message: String)
テスト (ScalaTest と Mockito を使用):
"Per Request Indexing Actor" should {
"send the HTTP Response when AddToIndexRequestCompleted message is received" in {
val request = mock[RequestContext]
val result = mock[Result]
val perRequestIndexingActor = TestActorRef(Props(new PerRequestIndexingActor(request)))
perRequestIndexingActor ! AddToIndexRequestCompleted(result)
verify(request).complete(result)
}
}
この行verify(request).complete(result)
は、暗黙的な Marshaller を使用しResult
て JSON に変換します。
追加することでマーシャラーをスコープに入れることができimplicit val marshaller: Marshaller[Result] = mock[Marshaller[Result]]
ますが、テストを実行するとマーシャラーの別のインスタンスが使用されるため、検証は失敗します。
モック Marshaller を明示的に渡してもcomplete
失敗します。
では、暗黙的なパラメーターのモック オブジェクトを作成し、インスタンスが使用されていることを確認する方法をアドバイスできますか?