-
Notifications
You must be signed in to change notification settings - Fork 111
Add have_json_subset/have_json_superset matcher #64
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
Conversation
|
|
Thinking more on the name, I mistook what you were implementing. a.should be_json_subset b # a should be a subset of b
a.should have_json_subset b # b should be a subset of aI wonder what is needed more. Once we implement one, the other can be easily implemented by simply calling the other. Personally I assume have_json_subset is more likely to be used as you are checking that at some specific key-value pairs are present. Have you found a specific time when be_json_subset is more specifically what you want? |
|
You pre-empted my next comment/PR.... when I was going to request that we add superset. But b.should be_json_subset a === a.should be_json_superset b
a.should have_json_subset b === a.should be_json_superset bI wonder too whether |
|
@charlierudolph - any thoughts/updates on this? |
|
I took a pass at something similar in #57. I originally agreed that it wasn't needed but I've hit some other use cases and we're actually using that branch in our application currently. |
|
Personally |
|
Would tend to agree with your comments on I will update my commits on this PR. |
|
I also agree. That's a cleaner way of thinking about it. |
|
I've revisited this with the following in mind:
Consider two 'bits' of JSON, a = %({ "apple": "green" })
b = %({ "apple": "green", "banana": "yellow" })
expect(a).to be_json_subset_of b
expect(b).to be_json_superset_of aUsing both superset and subset we can, as shown, expect the actual to be a subset of the expected without needing to expect the expected to be a subset of actual (if that makes sense) Updated commits pushed Thoughts? |
|
This still feels backwards to me. When I'm testing our json api, I usually want to assert that the response has an object which has a particular json subset in it. expect(response.body).to have_json_subset({id: 123}.to_json)Your proposed naming would lead to: expect({id: 123}.to_json).to be_json_subset_of response.body
# or
expect(response.body).to be_json_superset_of({id: 123}.to_json)The |
|
@ryansch - thanks for the feedback. After a more careful reading of the link, I'm going to take back my comments in point 2 above. So will move |
|
😄 |
|
And with that commit we are hopefully done :-) a = %({ "apple": "green" })
b = %({ "apple": "green", "banana": "yellow" })
expect(b).to have_json_subset a
expect(a).to have_json_superset bThoughts welcomed |
|
Code looks good. The main thing I'm wondering is do we need the |
|
Quite agree we could get away with one. But in some situations I need one and not the other so my test code 'looks' (and I realise that's very subjective) more readable. For example: it 'should work' do
example = Fabricate.to_params(:example)
response = make_request req_body: example.to_json
# consistency in terms of expectations if
# we can use either subset/superset
expect(response.status).to eq 201
expect(response.body).to have_json_subset example.to_json
# as opposed to (slightly contrived in the case of my example)
example = Fabricate.to_params(:example)
response = make_request req_body: example.to_json
expect(response.status).to eq 201
expect(example.to_json).to have_json_superset response.body
# ^^^^^^^^^^^^^^^ <- 'wrong' way round -> ^^^^^^^^^^^^^
endMake sense? If it's cheap to keep both then that would be my vote. |
|
Okay. Can you give me an example where |
|
Hands up - no, I can't, not right now :) Largely because all our JSON tests are linked to testing an API! So you keen to leave |
|
Yep that sounds good to me |
|
Ok - this finally should be done. Sorry for the delay |
|
Closing as this is very old... |
With due reference to #61 and #62
This is a first cut of a new
be_json_subsetmatcher.Tests and potential update to README included.
The one thing I found whilst doing this (leaning heavily on the tests for
be_json_eql) is that the notion ofat_pathreads one way be is interpreted another. Here is a working test against the new matcher:The following pseudo code however reads better given what was intended:
So whilst I have implemented
at_path(again leaning heavily on thebe_json_eqlimplementation) it doesn't read well to my mind. But this is really a separate issue.Thoughts welcomed on the PR.