jsonapi-rb / jsonapi-rspec

RSpec matchers for JSON:API spec
https://rubygems.org/gems/jsonapi-rspec
MIT License
123 stars 24 forks source link

Attributes matcher #14

Closed stas closed 4 years ago

stas commented 4 years ago

What is the current behavior?

There's a conflicting matcher named have_attributes, with the rspec-expectations gem.

What is the new behavior?

The old matcher is now have_jsonapi_attributes.

Support for #12 was also added.

Checklist

Please make sure the following requirements are complete:

stas commented 4 years ago

@gabrielgasser @mkamensky I will appreciate your feedback on this please. :bowing_man:

mkamensky commented 4 years ago

@gabrielgasser @mkamensky I will appreciate your feedback on this please. bowing_man

Thanks. As far as I understand, this only allows to check that the list of attributes is precisely the given one. In my case, I would like to make sure it is a subset, but I might not know in advance the full list of attributes.

stas commented 4 years ago

@mkamensky by default it allows to check only a subset, use .exactly to make sure all the attributes are included. Please take a look at the test-case:

https://github.com/jsonapi-rb/jsonapi-rspec/pull/14/files#diff-f4935a081f43d0f9f6f64106e74e4942R3-R24

mkamensky commented 4 years ago

But what I need is the other inclusion: I wish check that the actual attributes are contained in the ones I provide

stas commented 4 years ago

@mkamensky I don't understand. Could you explain what exactly is not working here?!

have_jsonapi_attributes(*keys) behavior is to check that the keys are present in the JSONAPI document have_jsonapi_attributes(*keys).exactly behavior is to check that no other except the keys in the list are present in the JSONAPI document

mkamensky commented 4 years ago

@stas I would like

expect(document['data']).to have_jsonapi_attributes_in(:name, :email)

to pass if the only attribute in the document is :name, and to fail if the attributes are :name and :country. How can I achieve this with your approach?

stas commented 4 years ago

to pass if the only attribute in the document is :name, and to fail if the attributes are :name and :country. How can I achieve this with your approach?

@mkamensky this doesn't make sense to me. What are you trying to achieve here?

As a solution, just use have_attribute(:name) and that's it!

stas commented 4 years ago

to pass if the only attribute in the document is :name, and to fail if the attributes are :name and :country. How can I achieve this with your approach?

You can also just use have_jsonapi_attributes(:name).exactly, this last .exactly will strictly check that there are no other attributes. See my example above.

mkamensky commented 4 years ago

The api might return different results, depending on the situation, and I do not wish to analyse precisely what that will. I only want to make sure that it does not return attributes the user is not authorized to see

mkamensky commented 4 years ago

Anyway, I'm not insisting on including it if you don't find it useful, I can simply keep it for my project

stas commented 4 years ago

@mkamensky I'm sorry but I'll be closing your requests. We already provided you with a couple of ways to solve your issue. But we can not accept and maintain some very specific matchers based on the users business logic.

Please consider either using have_attribute(:name) or not_to have_attribute(:YOUR_UNAUTHORIZED_ATTR_NAME). Same is true for a list of attributes not_to have_jsonapi_attributes(:YOUR_UNAUTHORIZED_ATTR_NAME1, :YOUR_UNAUTHORIZED_ATTR_NAME2)