collectiveidea / json_spec

Easily handle JSON in RSpec and Cucumber
rubygems.org/gems/json_spec
MIT License
919 stars 114 forks source link

IncludeJson matcher doesn't properly match arrays at a path. #32

Closed michalorman closed 12 years ago

michalorman commented 12 years ago

I've committed a fix for IncludeJson matcher so that arrays at a path are properly matched.

laserlemon commented 12 years ago

This is somewhat related to #31. I think this comes from a misunderstanding of the intent of the "include" matcher. The matcher only passes if the JSON (array or hash) includes the given element. So if you give an array argument, the JSON must include that array as an element.

To illustrate:

[1, 2, 3] does not include [1, 2]. [1, 2, 3] does include 2. [[1, 2], 3] does include [1, 2].

In terms of evaluating inclusion at a path, the rules are the same:

{"one": [2, 3], "four": [5, 6]} does not include [2, 3] at path "one". {"one": [2, 3], "four": [5, 6]} does include 2 at path "one". {"one": [2, 3], "four": [5, 6]} does include [2, 3].

What I think you may be looking for is:

%({"one": [2, 3], "four": [5, 6]}).should be_json_eql(%([2, 3])).at_path("one")

What you're describing is equality, not inclusion. I hope that helps clear it up! Thank you for the pull request.

michalorman commented 12 years ago

Great, that was exactly what I was looking for. Thanks!