collectiveidea / json_spec

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

Allow matching out-of-order arrays with .in_any_order #83

Open rusterholz opened 9 years ago

rusterholz commented 9 years ago

My team needed a way to reliably, consistently test the JSON returned by our API, despite the facts that:

For example, our API might respond with {"objects":[{"id":1,"some_attribute":"foo"},{"id":2,"some_attribute":"bar"}]} and that would be totally valid, but it is just as likely to respond with {"objects":[{"id":2,"some_attribute":"bar"},{"id":1,"some_attribute":"foo"}]} instead, and that should also be valid.

To allow the be_json_eql matcher to match out-of-order arrays, I've added the chain method .in_any_order. It works by sorting each array in lexical order according to the JSON string representation of the element.

You can also pass false to .in_any_order if you want to, which then behaves as if you had not chained .in_any_order at all (in case someone needs to determine indifference dynamically at runtime).

Examples are available in the modified be_json_eql_spec.rb file. I also added some tests to ensure that this feature should work correctly when combined with the other features of this matcher.

aliismayilov commented 9 years ago

Our team also need this functionality. :+1: to be merged

rusterholz commented 9 years ago

@aliismayilov It looks like the project managers haven't touched this repository in a few months... so, if you like, you can just use my fork for now!

aliismayilov commented 9 years ago

thanks @rusterholz !!

rusterholz commented 9 years ago

Bump!

I incorporated your feedback from above in my latest commit.

opsb commented 8 years ago

This would be very useful

madis commented 5 years ago

Is there an alternative for this functionality or plans to merge it?

askreet commented 4 years ago

Stumbled on this today, what a fine vintage this PR is. Would be useful to us as well.

jfo84 commented 4 years ago

Also stumbling across this just now. This would be very useful functionality.

jji-fieldwire commented 3 years ago

+1 from me too