Playframework Scala Specs2 JSON Matchers

I am using Play! and trying to work with JSON response messages in Specs2 tests without success.

What I'm trying to do is validate the key-> value pairs in JsValue, as in the example below ... but I can't get the correct transfers.

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

The errors I get

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

and

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

Not very useful, I think this is something simple that I am missing (new to both Scala and Play)

Steve

+4
source share
1 answer

JsonMatchers in specs2 should be tightened a bit. They are Matcher[Any] , where Any should have a toString method that can be parsed using Scala json parser (and not to play one).

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 the Play Json values ​​returns something slightly different than expected. This will be fixed in the next release of specs2.

+5
source

All Articles