collectiveidea / json_spec

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

Adding loose comparison -- code, and pull request if interested. #50

Open jejacks0n opened 11 years ago

jejacks0n commented 11 years ago

I wanted a loose comparison type of thing so I threw one together. I'm wondering if this is a feature worth implementing, and if so, I'll submit a pull request with proper specs and an adjusted matcher name -- if not someone else might find this useful.

gist: https://gist.github.com/jejacks0n/6384919

This allows specifying attributes that you want to ensure are there, but that you don't really care about the value of by using a wildcard (*).

eg.

expect(response_body).to look_like <<-JSON
  {
    "access_token": "*",
    "token_type": "bearer",
  }
JSON

Thoughts / feelings? If it's something worth adding, what would you like me to change?

laserlemon commented 11 years ago

I'm leery of the syntax because the "*" syntax is perfectly valid JSON. Plus this is possible using a different combination of matcher, although it might not be as short. I'd like to hear others' thoughts.

jejacks0n commented 11 years ago

Yeah, that's why I'm posting it as a question instead of spending time on the pull request first.. I find it useful -- I'm using RSpec API Documentation (which is great), and wanted the specs to be a little more readable.. it's usefulness is limited for sure, but I think there's value in having the spec read nicely as well as the documentation.

I've used something like this on a different project in cucumber and actually found it to be much more useful there.. My RSpec example is how I'm using it, but I think there's an argument for it. For instance, I don't like the idea of excluding created_at and updated_at, etc.. at a global level.. I want to document as well as ensure that they're in the response, I just don't care what their value is. The current library only supports excluding them entirely and this allows for them to be asserted as being there.

jejacks0n commented 11 years ago

If there's a different way to accomplish this I would like to know -- I dug around but didn't see anything obvious.

Thanks for the good library btw. =)

laserlemon commented 11 years ago

If you don't care about the values, you could achieve the same result using the have_json_path matcher. Or if you're using pure RSpec, you can use RSpec's built-in include matcher which does hash subset matching. json_spec is most useful in Cucumber.

mathieujobin commented 7 years ago

I would need something similar, where I want to ignore value for a few keys

right now we are using exclude_keys but this completely ignore the presence of the keys. I need the key to be present, but do not want to deal with varying values