Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

netty - Proper syntax for using match in Scenario

I'm using Karate for my mock service, so my karate files have Scenarios that include the params I need to match to run that scenario. I've had success with things like: pathMatches('my/api/path') && methodIs('post') && (karate.match("json.array[*].key contains null").pass

I now want to add something like 'or json.array[*].key == #isnotpresent' based on what I found in this article: https://github.com/intuit/karate/issues/270

It looks like you use the match keyword in the body of a test, but how could I do this in the Scenario? Use karate.match()? If so, what's the proper syntax? I know I'll need to do this kind of stuff more, so I want to get a handle on it.

I tried something like: karate.match('json.array[*] contains { key: 'notpresent'}').pass and karate.match('json.array[*] == {key: '#notpresent'}').pass without success.

I'm using 0.9.6 version of Karate that I found when I asked this question: Matching key value pairs in a request in a Karate Netty scenario

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here are two tips:

a) Test your Scenario expressions in a normal Karate test like this:

* def temp = 
"""
{
    "Array": [

        {
            "Id": "legitId"
        },
        {
            "foo": "bar"
        }
    ],
}
"""
#  
* assert karate.match("temp.Array[*].Id contains null").pass || !karate.match("each temp.Array contains { Id: '#present' }").pass

b) You can define functions in the Background and use in Scenario expressions:

Background:
* def isIdMissing = function(){ return karate.match("request.Array[*].Id contains null").pass || !karate.match("each request.Array contains { Id: '#present' }").pass }

Scenario: pathMatches('my/api/path') && methodIs('post') && isIdMissing()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...