collectiveidea / json_spec

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

Add #only to whitelist keys #57

Closed ryansch closed 10 years ago

ryansch commented 10 years ago

In my integration request specs, I sometimes want to find out if a json response contains objects with specific ids. With the existing interface I have to go through and explicitly blacklist all of the other keys. I instead added a whitelist with the #only method.

Example:

# include_json
%([{"id":1,"json":"spec"}]).should include_json(%({"id":1})).only(:id)

# be_json_eql
%({"id":1,"json":"spec"}).should be_json_eql(%({"id":1})).only(:id)

The actual json responses I'm comparing have far more keys.

charlierudolph commented 10 years ago

This can actually be done with the current interface

First example:

%([{"id":1,"json":"spec"}]).should be_json_eql(1).at_path("0/id")

Second example:

%({"id":1,"json":"spec"}).should be_json_eql(1).at_path("id")
ryansch commented 10 years ago

picard-facepalm