简体   繁体   English

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

[英]Akka-http: how to mock ActorMaterializer in unit test

I have a class A(httpClient: HttpExt)(implicit val system: ActorSystem, materializer: ActorMaterializer) that has a method called extractInfo (res: Future[HttpResponse]): Int that takes a future response and extracts an int value from the response entity. 我有一个class A(httpClient: HttpExt)(implicit val system: ActorSystem, materializer: ActorMaterializer)class A(httpClient: HttpExt)(implicit val system: ActorSystem, materializer: ActorMaterializer)具有名为extractInfo (res: Future[HttpResponse]): Int接受将来的响应并从响应中提取int值实体。

I want to write a unit test for this method and I have the following in my unit spec class: 我想为此方法编写一个单元测试,并且在我的单元规范类中有以下内容:

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>)
    }
  }
}

However, on the line mock[ActorMaterializer] , I got the following compilation error: 但是,在mock[ActorMaterializer]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]

I would love some suggestions on how to solve this problem. 我希望就如何解决此问题提出一些建议。 Thank you enormously in advance! 提前非常感谢您!

I would suggest not mocking the ActorMaterializer but instead using a default implementation for example ActorMaterializer() or the one you use for your production code. 我建议不要嘲笑ActorMaterializer,而应该使用默认实现,例如ActorMaterializer()或用于生产代码的默认实现。 At least from your example it doesn't look that you are making assertions on the ActorMaterializer itself. 至少从您的示例来看,您似乎并没有在ActorMaterializer本身上进行断言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM