collectiveidea/json_spec — pull request #83 by rusterholz (2014-10-24 02:47:48+00:00)
Title: Allow matching out-of-order arrays with .order_indifferent

My team needed a way to reliably, consistently test the JSON returned by our API, despite the facts that:
* the controller that builds the JSON might retrieve models in *any* order (not deterministic), and
* the client that will consume the JSON does not care about the order of models in the array.

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 `.order_indifferent`. It works by sorting each array in lexical order according to the JSON string representation of the element.

You can also pass `false` to `.order_indifferent` if you want to, which then behaves as if you had not chained `.order_indifferent` 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.