Karate framework – How to check conditional OR in match contains response



I am trying to use a match contains to verify my schema response and data types and sometimes it returns a null and sometimes it will return a string, as an example. I am trying the following but I am getting the assertion failed because it did not evaluate to true.

I am trying the following:

* match each $response.data.Results contains

        """
        {
        "providerID": '#number',
        "firstName": "#? _ == '#string' || _ == '#null'",
        "lastName": '#string',
        "mI": "#? _ == '#string' || _ == '#null'",
        "title": '#string',
        "name": '#string',
        "nameLFMT": '#string',
        "status": '#string',
        "specialties": '#array',
        "locations": '#array',
        "institutions": '#array',
        "acceptNewPatient": '#string',
        "imageUri": '#string',
        "nearestLatitude": '#number',
        "nearestLongitude": '#number'
        }

            """

The data returned for “firstName” for example is “firstName”:null,

Prior to the match each I am sending this:

Scenario: SearchResults
  #Verify 200 response status returned
Given text query =
        """
     {
                  Results: getSearchResults(searchLatitude:"48.942833",
                        searchLongitude: "-119.984549",
                        providerType: "Primary Care Physicians",
                        sortBy: "distance",
                        maxDistance:"600",
                        skip: 0,
                        take: 10) {
                                        providerID
                                        firstName
                                        lastName
                                        mI
                                        title
                                        name
                                        nameLFMT
                                        status
                                        specialties
                                        locations
                                        institutions
                                        acceptNewPatient
                                        imageUri
                                        nearestLatitude
                                        nearestLongitude
                        }

    }

        """

And request { query: '#(query)' }
When method post
Then status 200

I am not defining the schema, I have yet to figure out how to do this so I am not sure if that is my issue. I know this is probably how I should do this but I’m still learning.

Thanks for any help.

Answer

Ok I see one problem here:

"firstName": "#? _ == '#string' || _ == '#null'"

That’s not going to work. The part that follows the #? has to be a valid JavaScript expression. This may be hard to understand if you are not familiar with JS but #string and friends are specific to the Karate match keyword and not something that works in JavaScript.

So what the above line is doing is checking if the value of firstName is equal to the literal string #string.

Fortunately Karate has a solution: (refer doc for ‘fuzzy matching‘):

"firstName": "##string"

Which is a convenience, the double ## means ‘optional’ that will match for both a string or null.

EDIT: for advanced cases, please read this: https://stackoverflow.com/a/50350442/143475



Source: stackoverflow