Skip to content
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

Checking JSON response when there is an object inside an array #435

Closed
kneeHigh opened this issue Feb 1, 2018 · 2 comments
Closed

Checking JSON response when there is an object inside an array #435

kneeHigh opened this issue Feb 1, 2018 · 2 comments

Comments

@kneeHigh
Copy link

kneeHigh commented Feb 1, 2018

I am getting an error when I try to validate a key:value pair of an object that is in an array. I am wondering if this is possible and if so, is there an example?

Here is a snippet of the JSON response that I am testing (I want to test for a valid namespace and name inside the name objects:

{
    "EntityDirectory": {
        "entry": [
            {
                "about": "some value:A0001",
                "name": {
                    "namespace": "Automobiles",
                    "name": "A0001"
                }
            },
            {
                "about": "some value:GM",
                "name": {
                    "namespace": "Automobiles",
                    "name": "GM"
                }
            },
            {
                "about": "some value:VD005",
                "name": {
                    "namespace": "Automobiles",
                    "name": "VD005"
                }
            }
          ]
      }
}

Here is my Frisby test:

describe('integration tests', function() {
  it('test response', function (done) {
    frisby
      .timeout(timeoutSeconds)
      .get(someURL)
      .expect('status', 200)
      .expect('header','content-type', 'application/json;charset=UTF-8')
      .expect('json', 'EntityDirectory.entry.?', {
            about: 'some value:A0001'
        })
        .expect('json', 'EntityDirectory.entry.*.name', {       // <--- this is the line in question
            namespace: 'Automobiles',
            name: 'GM'
        })
        .done(done);
    }, timeoutSeconds);
});

Here is the error:
F.......(node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): AssertionError: Response [ {"namespace":"Automobiles","name":"GM"} ] does not contain provided JSON [ {"namespace":"Automobiles","name":"A0001"} ]

I am using frisby version 2.0.11.

Is there a way to test the values of the namespace and name for each object in the entry array?

@H1Gdev
Copy link
Collaborator

H1Gdev commented Feb 2, 2018

@kneeHigh

use ?.

        .expect('json', 'EntityDirectory.entry.*.name', {       // <--- this is the line in question

to

        .expect('json', 'EntityDirectory.entry.?.name', {

* means 'EACH item in array should match'. (e.g. use in expect jsonTypes.)
? means 'ONE item in array should match'.

ref #407.

@kneeHigh
Copy link
Author

kneeHigh commented Feb 2, 2018

That worked. Thanks!

@kneeHigh kneeHigh closed this as completed Feb 2, 2018
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

No branches or pull requests

2 participants