collectiveidea / json_spec

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

Add with_content chain to have_json_path matcher #52

Closed clupprich closed 7 years ago

clupprich commented 11 years ago

This PR adds a with_content() chain to the have_json_path matcher, which compares the content at the path with the one provided. I've found myself regularly in the position where not only the fact that the JSON path existed was important to me, but also the content. Of course you could split up this expectation up into two independent ones, but I think the with_content() makes the thing a lot easier.

I'm not totally happy with the failure messages - open for comments!

mugwump commented 10 years ago

+1 for this PR! This is excactly what I've been missing from json_spec...

Although I must say, that this is only syntactic-sugar, as you can achieve the same using

be_json_eql(expected_content.to_json).at_path("/some/path/to/your/content")
charlierudolph commented 10 years ago

Very good point. This is possible in the current system but giving people multiple ways to do things is a ruby thing and in some instance this may read better.

One question I would like to bring up is what with_content should accept. Ruby or JSON? If it accepts JSON then the following would be interchangeable

be_json_eql(json).at_path(path)
have_json_path(path).with_content(json)

Whatever is decided, test coverage definitely needs to be increased. Need tests for content being other types (integer, float, boolean, string, array, and hash) as well as cucumber features as well.

clupprich commented 10 years ago

Thanks for the feedback! I guess accepting Ruby here would be the more common use case, but I'll play around with it and let you know, also updating the pull request with an increased test coverage.