Skip to content

Attributes matcher #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2020
Merged

Attributes matcher #14

merged 2 commits into from
Feb 15, 2020

Conversation

stas
Copy link
Collaborator

@stas stas commented Jan 21, 2020

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:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes /
    features)
  • All automated checks pass (CI/CD)

Sorry, something went wrong.

stas added 2 commits January 21, 2020 20:58

Verified

This commit was signed with the committer’s verified signature.
stas Stas

Verified

This commit was signed with the committer’s verified signature.
stas Stas
RSpec core matchers already have a `have_attributes` matcher.

The matcher is extended to check strictly the number of attributes.

Closes #4
@stas
Copy link
Collaborator Author

stas commented Jan 21, 2020

@gabrielgasser @mkamensky I will appreciate your feedback on this please. 🙇‍♂️

@mkamensky
Copy link

@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
Copy link
Collaborator Author

stas commented Jan 22, 2020

@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
Copy link

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

@stas
Copy link
Collaborator Author

stas commented Jan 23, 2020

@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
Copy link

@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
Copy link
Collaborator Author

stas commented Jan 23, 2020

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
Copy link
Collaborator Author

stas commented Jan 23, 2020

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
Copy link

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
Copy link

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

@stas
Copy link
Collaborator Author

stas commented Jan 23, 2020

@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)

@stas stas merged commit 69dadbf into master Feb 15, 2020
@stas stas deleted the attributes_matcher branch February 15, 2020 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants