2

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.

Peter
  • 4,752
  • 2
  • 20
  • 32
Scott MacDonald
  • 115
  • 1
  • 8

2 Answers2

1

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
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Hey, fair enough! If I read you correctly, your advice is, #1 evaluate if this kind of matching is actually needed, given this is a mock, #2 if it is, try to do it in JS functions declared in the Background, or #3 try to do it in the body of the Scenario? For #3, would that mean having maybe 1 scenario to match my path then have conditional logic in the scenario body to decide which feature to run? – Scott MacDonald Oct 26 '20 at 17:37
  • @ScottMacDonald yeah you could call another feature in theory for #3 but I would try to manage with some JS. do consider submitting an example, just to make sure we fix any problems, I really wonder now if `match()` would work outside the body – Peter Thomas Oct 26 '20 at 18:01
0

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!

Scott MacDonald
  • 115
  • 1
  • 8