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 for unordered arrays? #7

Closed alex-fedorov closed 8 years ago

benoittgt commented 9 years ago

I was ready to open an "issue" for that. I'm having random errors when running test with 2 objects.

Like this

describe 'get#index' do
    before { sign_in admin }
    let(:store_id) { store.id }
    let!(:user1) { create :user, organisation: organisation }
    let!(:user2) { create :user, organisation: organisation }
    let(:page) { 0 }
    let(:request) { get :index, store_id: store_id, page: page, format: :json }

    context 'with correct store id' do
      before { request }

      context 'index return' do
        it do
          expect(response.body).to include_json(
            users: [
              {
                id:          user1.id,
                last_name:   user1.last_name,
                group_count: 2
              },
              {
                id:          user2.id,
                last_name:   user2.last_name,
                group_count: 1
              }
            ]
          )
        end
      end
    end
  end

Sometimes user1 is created before sometimes user2. It's quite anoying. I thought that using json_expectation should match json without order. I don't know if it's a feature in rspec or a bug.

benoittgt commented 8 years ago

Since then I now specify the order of my json. It's always specify it.

But it should be great to have something like match_array or HashWithIndifferentAccess. Will try to have a look but it's seems over-skilled for my junior skill. :smile:

waterlink commented 8 years ago

The public API here seems like that:

.to include_json(
  users: UnorderedArray[
     ...
  ]
)

WDYT?

benoittgt commented 8 years ago

It should be awesome @waterlink

waterlink commented 8 years ago

Will try to have a look but it's seems over-skilled for my junior skill. :smile:

Actually, I have just implemented that, and while doing so, I felt quite embarrassed for the current state of the code :sweat_smile:

Probably I will find some time and make a 40-60% re-factor or re-write.

benoittgt commented 8 years ago

Thanks a lot. Looks good to me.

For the refactoring that's a big task.

waterlink commented 8 years ago

OK, I have released 1.3.0 with this feature. Docs can be found here: https://relishapp.com/waterlink/rspec-json-expectations/docs/json-expectations/unordered-array-matching-support-for-include-json-matcher