[英]Playframework Scala Specs2 JSON Matchers
I'm using the Play! 我正在使用Play! framework and trying to work with JSON response messages in Specs2 tests with no success.
框架并尝试在Specs2测试中使用JSON响应消息但没有成功。
What I am trying to do is assert key->value pairs in a JsValue like in the example below ... but I can't get the matchers to correctly pass. 我想要做的是在JsValue中断言key->值对,如下例所示...但我无法让匹配器正确传递。
import org.specs2.mutable._
import play.api.libs.json.{Json, JsValue}
class JsonSpec extends Specification {
"Json Matcher" should {
"Correctly match Name->Value pairs" in {
val resultJson:JsValue = Json.parse("""{"name":"Yardies"}""")
resultJson must /("name" -> "Yardies")
}
"Correctly match Name->Value pairs with numbers as doubles" in {
val resultJson:JsValue = Json.parse("""{"id":1}""")
resultJson must /("id" -> 1.0)
}
}
}
Errors I get are 我得到的错误是
{name : Yardies} doesn't contain '(name,Yardies)'
and 和
{id : 1.0} doesn't contain '(id,1.0)'
Not very helpful, I imagine that it is something simple I am missing (new to both Scala and Play) 不是很有帮助,我想这是我想念的简单事件(Scala和Play都是新手)
Steve 史蒂夫
The JsonMatchers
in specs2 should be tightened a little bit. JsonMatchers
中的JsonMatchers应该收紧一点。 They are Matcher[Any]
, where Any
is supposed to have a toString
method which can be parsed by Scala's json parser (and not Play's one). 它们是
Matcher[Any]
,其中Any
应该有一个toString
方法,可以由Scala的json解析器解析(而不是Play的解析器)。
The following specification works as expected: 以下规范按预期工作:
class JsonSpec extends Specification {
"Json Matcher" should {
"Correctly match Name->Value pairs" in {
val resultJson = """{"name":"Yardies"}"""
resultJson must /("name" -> "Yardies")
}
"Correctly match Name->Value pairs with numbers as doubles" in {
val resultJson = """{"id":1}"""
resultJson must /("id" -> 1.0)
}
}
}
In your case I suspect that parsing the toString
representation of Play's Json values returns something slightly different what the matchers are expecting. 在你的情况下,我怀疑解析Play的Json值的
toString
表示会返回与匹配者期望的略有不同的东西。 This will be fixed in the next specs2 release. 这将在下一个specs2版本中修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.