我有一个我想测试的 Controller 操作,但是我收到以下错误。我觉得有一些小东西我可能会丢失,但不能完全弄清楚它是什么。我写了一些小代码来帮助说明问题。
更多的上下文,我希望能够向 Controller 提供模拟依赖项,这就是我手动接管其实例化的原因。
java.lang.UnsupportedOperationException: NoMaterializer does not provide an ExecutionContext
[info] at play.api.test.NoMaterializer$.executionContext(Helpers.scala:661)
[info] at play.api.mvc.PlayBodyParsers.$anonfun$enforceMaxLength$1(BodyParsers.scala:866)
[info] at akka.stream.impl.Compose.apply(TraversalBuilder.scala:164)
[info] at akka.stream.impl.PhasedFusingActorMaterializer.materialize(PhasedFusingActorMaterializer.scala:488)
[info] at akka.stream.impl.PhasedFusingActorMaterializer.materialize(PhasedFusingActorMaterializer.scala:424)
[info] at akka.stream.impl.PhasedFusingActorMaterializer.materialize(PhasedFusingActorMaterializer.scala:415)
[info] at akka.stream.scaladsl.RunnableGraph.run(Flow.scala:496)
下面是我为帮助说明问题而编写的 Controller
case class Location(lat: Double, long: Double)
object Location {
implicit val locationReads: Reads[Location] = (
(JsPath \ "lat").read[Double](min(-90.0) keepAnd max(90.0)) and
(JsPath \ "long").read[Double](min(-180.0) keepAnd max(180.0))
) (Location.apply _)
}
class HomeController @Inject()(
cc: ControllerComponents,
mat: Materializer
)(implicit val ec: ExecutionContext) extends AbstractController(cc) {
def savePlace: Action[JsValue] = Action(parse.json) { request: Request[JsValue] =>
val placeResult = request.body.validate[Location]
placeResult.fold(
errors => {
BadRequest(Json.obj("status" -> "KO", "message" -> JsError.toJson(errors)))
},
place => {
Ok(Json.obj("status" -> "OK", "message" -> "Location saved"))
}
)
}
}
和测试代码
class HomeControllerSpec extends PlaySpec with Injecting with Inside with GuiceOneAppPerSuite with BeforeAndAfterEach with Results with BeforeAndAfterAll {
implicit lazy override val app: play.api.Application = new GuiceApplicationBuilder().configure().build()
implicit lazy val materializer: Materializer = app.materializer
implicit lazy val components = Helpers.stubControllerComponents()
"savePlace" must {
val api = new HomeController(components, mat = materializer)
val payload = Json.obj(
"location" -> Json.obj("lat" -> 51.235685, "lng" -> -1.309197)
)
"just demo" in {
val request = FakeRequest(POST, "/location").withJsonBody(payload)
val result = call(api.savePlace, request)
status(result) mustBe UNAUTHORIZED
}
}
}
我正在使用 Play 2.6.3
最佳答案
我回答这个问题是为了让其他可能面临同样问题的人受益。
为了解决这个问题,我没有注入(inject) stub Controller 组件(如 Helpers.stubControllerComponents()
),而是使用 inject[ControllerComponents]
.
基本上,为了解决这个问题, Controller 代码现在看起来像这样。
class HomeControllerSpec
extends PlaySpec with Injecting with Inside with GuiceOneAppPerSuite with Results {
implicit lazy override val app: play.api.Application = new GuiceApplicationBuilder().configure().build()
implicit lazy val materializer = app.materializer
implicit lazy val components = inject[ControllerComponents]
"savePlace" must {
val api = new HomeController(components)
val payload = Json.obj("lat" -> 51.235685, "long" -> -1.309197)
"just demo" in {
val request = FakeRequest(POST, "/location").withJsonBody(payload)
val result = call(api.savePlace, request)
println(contentAsJson(result))
status(result) mustBe UNAUTHORIZED
}
}
}
关于json - 具有 json 解析器的单元测试 Play Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46289476/