LoginSignup
10
10

More than 5 years have passed since last update.

Specs2でJsonマッチャーを使う

Posted at

Specs2 にはオプションも含めると多量のマッチャーがあるようです。僕が今日ちょっとJsonのマッチャーでハマったので、記事を残しておきます。

Json マッチャーを使うのに必要な準備

import org.specs2._
import specification._
import matcher.JsonMatchers

class HogeSpec extends script.Specification with Grouped with JsonMatchers

s2 interpolationを使わない場合は、

class HogeSpec extends Specification with JsonMatchers

でいけるはずです。僕としてはs2記法をおすすめしてみたりしてます。

今日はこの import と with JsonMatchers を知らずにハマってました…。

マッチさせる方法

val json = """{"hoge":"fuga","piyo":{"id":1}}"""

みたいなjsonがあった時に、

json must /("hoge" -> "fuga")

json must /("piyo") /("id" -> 1.0)

のようにマッチするといいようです。

また、文字列だけじゃなくて、Play-jsonのJsValueなんかもうまくマッチしてくれるようです。

詳しくは、http://etorreborre.github.io/specs2/guide/org.specs2.guide.Matchers.html のOptionalのJsonの項目を見てみてください。(僕には説明が割と意味不明でしたが。)

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10