Play Framework test helpers need implicit `Materializer`

若如初见. 提交于 2019-12-30 10:35:02

问题


I'm using Play 2.6.x and the test helper for status(result) has the method:
def status(of: Accumulator[ByteString, Result])(implicit timeout: Timeout, mat: Materializer): Int = status(of.run())

Running tests throws when the compiler can't find the implicit value: could not find implicit value for parameter mat: akka.stream.Materializer

What is the Materializer -- I'm assuming it's part of Akka-HTTP

And how can I provide one?


回答1:


From akka streams docs:

The Materializer is a factory for stream execution engines, it is the thing that makes streams run [...]

The Materializer is the cornerstone of Akka Streams, on which Akka HTTP is built on. You need one of these to be implicitly resolved to make your test compile.

Presently the ActorMaterializer is the only available implementation of Materializer. It is a Materializer based on Akka actors. This is the reason why, to create one, you need in turn to have an ActorSystem in scope.

The following code is what you need in your test:

implicit val sys = ActorSystem("MyTest")
implicit val mat = ActorMaterializer()



回答2:


there's also a status method in the form:

def status(of: Future[Result])(implicit timeout: Timeout): Int

make sure the controller return type is correct so the action returns a Future[Result]




回答3:


How about doing this:

implicit val materializer = ActorMaterializer()


来源:https://stackoverflow.com/questions/48036803/play-framework-test-helpers-need-implicit-materializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!