waterlink / rspec-json_expectations

Set of matchers and helpers to allow you test your APIs responses like a pro.
https://www.relishapp.com/waterlink/rspec-json-expectations/docs
MIT License
140 stars 23 forks source link

Support array size ? #11

Closed benoittgt closed 8 years ago

benoittgt commented 9 years ago

An idea will be to count the number of json object in array accessible.

Something like

it 'return 3 objects' do
  expect(subjet).to include_json_with_exact_size(
    users: [
        {
          groups: [
            {
             id: group_everybody.id,
              name: 'everybody'
            },
            {
              id: group.id,
              name: group.name
            },
            {
              id: group2.id,
              name: group2.name
            }
          ]
        )
end
Failure/Error: expect(subjet).to include_json_with_exact_size(

                 json atom size  "groups" is not equal to expected value:

                         expected: 3
                              got: 4
benoittgt commented 8 years ago

For the moment I'm doing a JSON.parse and expect size of my elements.

waterlink commented 8 years ago

Hi @benoittgt

Could you explain in detail the expected behavior of this matcher? How is it different from the currently implemented one?

benoittgt commented 8 years ago

Thinking a little bit more about this feature doesn't seems to be useful for rspec-json_expectations.

This gem verify that key and values are present not that the number of elements in an array is equal to something.

it 'have 4 element in the array' do
  expect(JSON.parse(response.body)[:key_of_the_array].size).to eq(4)
end

Seems enough to do it. No need to add this feature to the gem ! :)