Akka Http / Circe解码结果

时间:2019-02-07 14:47:41

标签: json scala akka-http circe

如果我错过了一些简单的事情,我深表歉意,但是我想在Circe中使用Akka HTTP(使用akka-http-json Circe模块)。我正在尝试在混合了ErrorAccumulatingCirceSupport特性的ScalaTest中检索GET调用的结果。调用成功完成,但是我无法解组响应...这是一个非常简单的测试,但是我不确定如何将结果解组到域对象列表中,例如:

    Get("/path/to/getfoos").withHeaders(auth) ~> Route.seal(service.route) ~> check {
      import io.circe.generic.auto._
      status shouldEqual StatusCodes.OK
      contentType should ===(ContentTypes.`application/json`)
      val reports = responseAs[List[Foo]]
      reports.size shouldBe 1
   }

我得到的错误是:

Could not unmarshal response to type 'scala.collection.immutable.List' for `responseAs` assertion: de.heikoseeberger.akkahttpcirce.ErrorAccumulatingCirceSupport$DecodingFailures: DecodingFailure at [0]: CNil

如果有人可以指出我错了,我将非常感谢您的帮助!

谢谢!

1 个答案:

答案 0 :(得分:0)

我不确定这是否是最好的方法,但是我可以使用类似下面的方法来解开案例类。如果有更好的方法,请告诉我!

      val entity = responseEntity
      val foos: List[Foo] = Unmarshal(entity).to[List[Foo]].futureValue
      foos(0) shouldBe expectedFoo
      foos.size shouldBe 1

(请注意,我还必须混入org.scalatest.concurrent.ScalaFutures特性)