Nitheesh hunsur
Nitheesh hunsur

Reputation: 161

Karate - Matching Part Json with Parent

Below is the json samples used for matching; match contains is not working.

* def Parent = [{ a: 1, b: 'x' }, { a: 2, b: 'y' }]
* def Part = [{ a: 1 }, { b: 'y' }]
* match Parent contains '#(^Part)'

# Below throws exception too.
* match Parent contains Part

Its throwing below error,

actual: [{"a":1,"b":"x"},{"a":2,"b":"y"}], expected: '#(^Part)', reason: actual value does not contain expected

Upvotes: 1

Views: 456

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Here is what I think is the best solution. Take your time to understand it ;)

* def data = [{ a: 1 }, { b: 'y' }]
* def response = [{ a: 1, b: 'x' }, { a: 2, b: 'y' }]
* def expected = karate.map(data, function(x, i){ return '#(^data[' + i + '])' })
* match response contains only expected

Since I expect this example to be useful as a reference to others and more, here is an alternative solution, which also showcases the new dynamic scenario outlines in 0.9.0. Note how karate.map() is one way to do loops, and there are other ways if you look at the docs and examples.

Background:
  * def data = [{ a: 1 }, { b: 'y' }]
  * def parts = karate.map(data, function(x){ return { part: x } })
  * def response = [{ a: 1, b: 'x' }, { a: 2, b: 'y' }]

  Scenario Outline:
    * def part = <part>
    * match response contains '#(^part)'

    Examples:
      | parts |

Upvotes: 1

Related Questions