have_json_fields matcher #25

wants to merge 1 commit into


None yet

2 participants


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))

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.

@laserlemon laserlemon closed this Apr 9, 2012

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? :)


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


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! 👏

@laserlemon laserlemon reopened this Apr 9, 2012

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

@laserlemon laserlemon closed this Oct 3, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment