[英]Scala testing mocking implicit parameters?
当涉及隐式参数时,我在尝试理解如何在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带入范围implicit val marshaller: Marshaller[Result] = mock[Marshaller[Result]]
但是当我运行测试时,使用了Marshaller的不同实例,因此验证失败。
甚至明确地传递模拟Marshaller complete
失败。
那么,任何人都可以建议如何为隐式参数创建一个模拟对象,并确保该实例是使用的实例?
这是一个完美的情况,使用Mockito的Matcher
作为marshaller arg。 你不应该嘲笑隐式编组。 你真正想做的就是验证调用complete
是否与你期望的result
匹配,以及marshaller的一些实例。 首先,如果您还没有这样做,请将Mockito匹配器带入范围,并使用如下导入:
import org.mockito.Matchers._
然后,如果你想在结果上进行参考匹配,你可以这样验证:
verify(request).complete(same(result))(any[classOf[Marshaller[Result]]])
或者,如果你想在结果上匹配等于你可以做到:
verify(request).complete(eq(result))(any(classOf[Marshaller[Result]]))
与匹配器的诀窍是,一旦你使用一对一ARG,你必须使用他们所有ARGS,所以这就是为什么我们必须使用一个result
了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.