Akka-http:如何在单元测试中模拟ActorMaterializer

时间:2017-09-29 00:27:27

标签: scala akka akka-http

我有一个class A(httpClient: HttpExt)(implicit val system: ActorSystem, materializer: ActorMaterializer),它有一个名为extractInfo (res: Future[HttpResponse]): Int的方法,它接受未来的响应,并从响应实体中提取一个int值。

我想为这个方法编写一个单元测试,我在我的单元规范类中有以下内容:

class ASpec extends UnitSpec with MockFactory {
  "An A" - {
    implicit val as = mock[ActorSystem]
    implicit val mat = mock[ActorMaterializer]
    val stubHttpClient = stub[HttpExt]
    val a = new A(stubHttpClient)

    "return auth token from response" in {
      val futureResponse: Future[HttpResponse] = <code that returns a Future[HttpResponse]>
      val info: Future[Option[Int]] = A.extractInfo(futureResponse)
      val result = Await.result(info, 10.seconds)
      result shouldBe Some(<a int value>)
    }
  }
}

但是,在mock[ActorMaterializer]行上,我收到了以下编译错误:

Error:(19, 28) object creation impossible, since:
it has 3 unimplemented members.
/** As seen from <$anon: akka.stream.ActorMaterializer>, the missing . 
signatures are as follows.
*  For convenience, these are usable as stub implementations.
*/
private[package akka] def actorOf(context: akka.stream.MaterializationContext,props: akka.actor.Props): akka.actor.ActorRef = ???
private[package akka] def logger: akka.event.LoggingAdapter = ???
private[package akka] def supervisor: akka.actor.ActorRef = ???
implicit val mat = mock[ActorMaterializer]

我想对如何解决这个问题提出一些建议。非常感谢你提前!

1 个答案:

答案 0 :(得分:0)

我建议不要模仿ActorMaterializer,而是使用例如ActorMaterializer()的默认实现或用于生产代码的默认实现。至少在你的例子中,它看起来你没有在ActorMaterializer本身上做出断言。