【问题标题】:How to test a JSON array from Spray GET response?如何从 Spray GET 响应中测试 JSON 数组?
【发布时间】:2023-03-26 21:08:01
【问题描述】:

Spray 表示任何普通数据类型,包括 Seq 都会自动(取消)编组到 JSON 或从 JSON 中获取。出于某种原因,我没有遇到这种情况。

import spray.json._
import DefaultJsonProtocol._
...

class RestAPITest
  extends FlatSpec
  with Matchers
  with ScalatestRouteTest
  with MyRoute
{
  ...

  behavior of "MyRoute"

  it should "return a list as JSON" in {
    Get("/computers") ~> myRoute ~> check {
      status should equal(OK)

      // 'sbt test' gives:
      // "could not find implicit value for evidence parameter of type spray.httpx.unmarshalling.FromResponseUnmarshaller[Seq[String]]"
      //
      responseAs[Seq[String]] should contain theSameElementsAs( List( "A01", "A02", "A03", "E01", "G04" ) )
    }
  }

我做错了什么?

【问题讨论】:

  • 尝试添加import spray.httpx.SprayJsonSupport._
  • 似乎有效,谢谢! (还有一些问题我明天会详细看到)
  • 这和升级到 Spray 1.3.0 有所帮助。 'responseAs' 行在其简单性中非常棒!去 spray-json 和 scala! :) 如果您将评论作为答案,我会很乐意批准。
  • here 所述,发布您的“附录”作为您自己问题的答案是个好主意。

标签: json scalatest spray


【解决方案1】:

最终将 spray.httpx.SprayJsonSupport 特征混合到我的测试类中,以明确发生了什么。这件作品简直就是一颗宝石——使得测试 JSON REST API 的本质上是单行的!快乐。

http://spray.io/documentation/1.1-SNAPSHOT/spray-httpx/spray-json-support/

谢谢@jrudolph :)

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 2019-04-28
    • 2017-10-08
    • 2020-12-16
    • 1970-01-01
    相关资源
    最近更新 更多