在 Scala 中使用 spec2 测试 JSON



我正在尝试使用 Specs2 测试一个 Json,但我总是收到解析错误。

也许是因为我使用 JObect?

val j: JObject = "hello" -> "world"
j must */("hello")

这是错误:

Search_fields

Could not parse:
JObject(List(JField(hello,JString(world))))
java.lang.Exception: Could not parse:
JObject(List(JField(hello,JString(world))))
    at net.liftweb.echidnasearch.QuerySearchSpec$$anonfun$1$$anonfun$apply$124.apply(QuerySearchSpec.scala:496)
    at net.liftweb.echidnasearch.QuerySearchSpec$$anonfun$1$$anonfun$apply$124.apply(QuerySearchSpec.scala:485)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
MatchQuery

谢谢

specs2 json matchers 是Matcher[String] .我怀疑您所看到的是尝试解析 Lift Json 对象的.toString表示形式,该表示形式无法被 specs2 匹配器解析。您应该改为与 Lift Json 对象的适当字符串表示形式进行匹配。

如果您的意思是编译错误,请检查所有导入并重试:

import net.liftweb.json.JsonDSL._
import net.liftweb.json._
val t:JObject = ("a" -> "b")

我在控制台中尝试了这个,它有效:

scala> val t:JObject = ("a" -> "b")
t: net.liftweb.json.JObject = JObject(List(JField(a,JString(b))))

最新更新