Schema Matching in Scenarios

Viewed 73

I'm trying to use karate.match() to check the schema on a request in the Scenario.

Here's a sample schema I'm declaring in my Background:

* def mySchema = {place: { id: '#string',},id: '#string',number: '#number'}

I asked here about how to to match the full schema: Schema validation in the Scenario

It was suggested that this was a duplicate, and that this reference for matching would do the trick: Is it possible to use karate 'match' inside conditional statement?

I'm still having trouble, and I don't think that the reference suggested applied in my case, because it seems to talk only about using match in a test, but the syntax seems to be different when used in a Scenario.

For instance, I decided to try to only match one part of my schema to just make sure I was using the correct syntax. I sent this request:

{"place": {"id": "someID"},"id": "someOtherID","number": 10}

I decided to try to match on the number value.

This is my Scenario:

Scenario: pathMatches('my/api/path') && !karate.match("request contains { number: '#number'}").pass

I also tried

!karate.match("each number =='#number'}").pass

bit neither work as intended. I think in my request the number key value is a number. That being the case, I'd expect the ! to cause this scenario to be skipped, but it's not, which leads me to believe my schema match syntax isn't correct.

I appreciate any help.

2 Answers

Sorry your question is hard to read, so I suggest you follow this process if you really want help: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue - and who knows, maybe it is a bug - and you can help up improve the framework.

I have certainly never tried these extreme matches in mocks, and I really wonder why this is needed. Karate is a "mock" not a full-blown server-side framework. BTW we will add a new way to do mocks in the future / 1.0

That said, 2 tips:

  • as I said in a previous answer, try using functions set up in the `Background, fall-back to "brute force" JS if needed
  • keep in mind that you don't need to do all the work in the Scenario expression. Once within the "body" of the Scenario you can use JS to do if-then-else kind of logic

After a lot of tooling around I was able to make progress on this when I realized the main problem was with me and how I was sending the request. The major issue was that Postman was sending the request as a GraphQL query. I'm new to Postman, but it's a bit of dumb-dumb mistake. Here's hoping someone else can learn from my mistake!

Related