collectiveidea / json_spec

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

`include_json` matcher doesn't introspect arrays of objects #65

Closed bencates closed 10 years ago

bencates commented 10 years ago
expect(%([{"foo": "bar"}])).to include_json(%("bar"))

Should pass but doesn't.

laserlemon commented 10 years ago

Inclusion is one level deep.

expect(%([{"foo":"bar"}])).to include_json(%({"foo":"bar"}))

will pass. Your example is intended to fail.

bencates commented 10 years ago

Fair enough, but I'm not exactly sure how to go about testing my use case. Perhaps you could give me a little guidance.

I've got a record that looks like this, but with many more irrelevant fields removed:

{
  "people": [{
    "id": 1,
    "address": {
      "street1": "123 Main St.",
      "city": "Anytown",
      "state": "CA",
      "zip": 90210
    }
  }]
}

I'm trying to unit test the JSON of the embedded address hash without having to specify the rest of the person record. I was wanting to write something like this:

expect(response.body).to include_json(%(
  {
    "street1": "123 Main St.",
    "city": "Anytown",
    "state": "CA",
    "zip": 90210
  }
)).at_path('people/0/address')

Any thoughts on how to rewrite that test so it'll work?

charlierudolph commented 10 years ago

You test could be exactly as you're written it but use the matcher be_json_eql instead of include_json

bencates commented 10 years ago

/headdesk

Thanks.