Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
wants to merge 1 commit into from

2 participants

@michalorman

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

@laserlemon
Owner

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.

@laserlemon laserlemon closed this
@michalorman

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
4 lib/json_spec/matchers/include_json.rb
@@ -16,7 +16,9 @@ def matches?(actual_json)
expected = exclude_keys(parse_json(@expected_json))
case actual
when Hash then actual.values.map{|v| exclude_keys(v) }.include?(expected)
- when Array then actual.map{|e| exclude_keys(e) }.include?(expected)
+ when Array
+ actual = actual.map{|e| exclude_keys(e) }
+ actual.include?(expected) || actual == expected
else false
end
end
View
12 spec/json_spec/matchers/include_json_spec.rb
@@ -45,10 +45,20 @@
json.should include_json(%([5,6]))
end
- it "matches at a path" do
+ it "matches a hash at a path" do
%({"one":{"two":[3,4]}}).should include_json(%([3,4])).at_path("one")
end
+ it "matches an array of elements at a path" do
+ json = %({"one":[2,3],"four":[5,6]})
+ json.should include_json(%([2,3])).at_path "one"
+ json.should include_json(%([5,6])).at_path "four"
+ end
+
+ it "matches an array of hashes at a path" do
+ %({"one":1,"two":[{"three":3},{"four":4}]}).should include_json(%([{"three":3},{"four":4}])).at_path("two")
+ end
+
it "ignores excluded keys" do
%([{"id":1,"two":3}]).should include_json(%({"two":3}))
end
Something went wrong with that request. Please try again.