Tim Keating
Tim Keating

Reputation: 6641

Is there a way to match dynamic object keys?

I'm looking for a simple technique to match objects where the key may not be known in advance (e.g. we may fetch the schema as part of the test). As a contrived example:

    Scenario:
        * def result = { foo: 'bar' }
        * def key = 'foo'

        Then match result == { '#(key)': 'bar' }

...which doesn't currently work.

Upvotes: 1

Views: 174

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58088

Once you realize there is a JavaScript engine behind the scenes, you will get even more ideas :)

* def result = { foo: 'bar' }
* def key = 'foo'
* def expected = {}
* expected[key] = 'bar'
Then match result == expected

Also do a search for other answers [karate] dynamic you will find many interesting examples such as this one: https://stackoverflow.com/a/57226061/143475

Upvotes: 4

Related Questions