加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Playframework Scala Specs2 JSON Matchers

发布时间:2020-12-16 10:03:51 所属栏目:安全 来源:网络整理
导读:我正在使用Play!框架并尝试在Specs2测试中使用 JSON响应消息但没有成功. 我想要做的是在JsValue中声明key-值对,如下例所示……但我无法让匹配器正确传递. import org.specs2.mutable._import play.api.libs.json.{Json,JsValue}class JsonSpec extends Spec
我正在使用Play!框架并尝试在Specs2测试中使用 JSON响应消息但没有成功.

我想要做的是在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)
    }
  }
}

我得到的错误是

{name : Yardies} doesn't contain '(name,Yardies)'

{id : 1.0} doesn't contain '(id,1.0)'

不是很有帮助,我想这是我想念的简单事件(Scala和Play都是新手)

史蒂夫

解决方法

规格2中的JsonMatchers应该收紧一点.它们是Matcher [Any],其中Any应该有一个toString方法,可以由Scala的json解析器解析(而不是Play的解析器).

以下规范按预期工作:

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)
    }
  }
}

在你的情况下,我怀疑解析Play的Json值的toString表示会返回与匹配者期望的略有不同的东西.这将在下一个specs2版本中修复.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读