collectiveidea / json_spec

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

have_json_fields matcher #25

Closed inossidabile closed 12 years ago

inossidabile commented 12 years ago

This matcher allows to check existence of fields set ignoring their values. Can be useful for highly-random large hashes acceptance testing.

%({"a": "a", "b": "b", "c": "c"}).should have_json_fields(%w(a b c))
laserlemon commented 12 years ago

This can be accomplished using the have_json_path matcher. For your example:

json = %({"a": "a", "b": "b", "c": "c"})
json.should have_json_path("a")
json.should have_json_path("b")
json.should have_json_path("c")

I hope that helps! Thank you for the pull request, but I think the existing functionality will suffice.

inossidabile commented 12 years ago

And now imagine that with "foo/bar/0/a", "foo/bar/0/b" etc. And for 20 fields. Like 10x more code to write. Are you quite sure? :)

inossidabile commented 12 years ago

I mean with have_json_path you can not use .at_path. With the new matcher you are supposed to.

laserlemon commented 12 years ago

That's a good point. There are a few things I would change. To be consistent with JavaScript objects and Ruby hashes, I would rename the matcher to have_json_keys. I think it also needs some more bulletproof testing. And it would be great to have a Cucumber step that exposes the new matcher. Then to top it all off, the README would need some love to explain the new features.

Please feel free if you want to take a stab at those things. Otherwise, I will get to pulling this in and making those changes when I'm able.

Thank you! :clap:

inossidabile commented 12 years ago

Yep, makes sense. I'll handle that tomorrow :)