Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Make include_json a subset relation #62

wants to merge 1 commit into from

2 participants


Rewrite of #61 which included a wrong branch name and commit message


Since you are looking for specifically a subset test, how about a new matcher instead of adding this into the include_json matcher. Maybe have_json_subset which returns false if actual is not a hash, otherwise returns whether or not expected is a subset of actual. The matcher should also allow at_path like a few of the other matchers. What do you think?


Sounds good. I suspect that's probably the best way to go if you want to retain the include semantics.

Want me to go ahead and add the implementation to my pull request?


Actually, probably makes more sense to close this PR and create another one with correct naming (given that we are no longer going to modify include_json)


Yeah, probably cleaner as a fresh PR. Thank you.

@myitcv myitcv closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 2, 2013
  1. @myitcv
This page is out of date. Refresh to see the latest.
4 lib/json_spec/matchers/include_json.rb
@@ -15,7 +15,9 @@ def matches?(actual_json)
actual = parse_json(actual_json, @path)
expected = exclude_keys(parse_json(@expected_json))
case actual
- when Hash then{|v| exclude_keys(v) }.include?(expected)
+ when Hash then
+ expected.is_a?(Hash) && { |k,v| expected.include? k } == expected ||
+{|v| exclude_keys(v) }.include?(expected)
when Array then{|e| exclude_keys(e) }.include?(expected)
when String then actual.include?(expected)
else false
10 spec/json_spec/matchers/include_json_spec.rb
@@ -45,6 +45,16 @@
json.should include_json(%([5,6]))
+ it "is a subset relation" do
+ json = %({"one":1})
+ json.should include_json(%({"one":1}))
+ end
+ it "matches where expected is a proper subset of actual" do
+ json = %({"one":1, "two": 2})
+ json.should include_json(%({"one":1}))
+ end
it "matches a substring" do
json = %("json")
json.should include_json(%("js"))
Something went wrong with that request. Please try again.