Akka Route TestKit无法将响应解编集为字符串

plicqrtu  于 2022-11-05  发布在  其他
关注(0)|答案(1)|浏览(104)

我有以下测试:

"fail if date is wrongly formatted" in {
    val endpoint = s"/api/prof?date=wrongdate"
    Get(endpoint) ~> check {
      status shouldBe StatusCodes.BadRequest
      val resp = responseAs[String]
      resp shouldBe "could not be parsed"
    }
 }

但是,测试失败,原因如下:

Could not unmarshal response to type 'java.lang.String' for `responseAs` assertion: akka.http.scaladsl.unmarshalling.Unmarshaller$UnsupportedContentTypeException: Unsupported Content-Type [Some(text/plain; charset=UTF-8)], supported: application/json

Response was: HttpResponse(400 Bad Request,List(),HttpEntity.Strict(text/plain; charset=UTF-8,106 bytes total),HttpProtocol(HTTP/1.1))

如何将响应主体作为字符串获取?

qc6wkl3g

qc6wkl3g1#

假设您在作用域中有一个针对JSON的隐式解组器,因此在测试中会选择它作为解组器。
添加类似于

implicit val responseBodyUnmarshaller =
  Unmarshaller.strict[HttpResponse, HttpEntity](_.entity)
    .andThen(Unmarshaller.stringUnmarshaller)

应该解决这个问题。

相关问题