While testing an API, often you need to iterate some actions over a collection of items; to do so, there is the ‘each’ operator that checks if each element in the collection satisfies the assertion you have written. When the collection is small (few elements), that operator can be substitute with a special iterator, the indirection. The major differences between each and indirection are:
- ‘each’ operator is recommended when you have to do lot of operations
- ‘each’ operator is recommended if you need a detailed report when there are errors: each element that has been evaluated will be listed in the test report, this won’t be done with the indirection.
- ‘each’ operator can contain logic in the expression field (e.g. <each expression=”item==’true'”>)
- indirection is faster and more compact
The indirection can be used in all assertion in the ‘expression’ field and allows you to iterate on all values of a collection.
|<assert-contains expression=”payload.items->color” value=”blue” mode=”all”/>|
All elements ‘color’ in the ‘items’ collection must contain ‘blue’
You might want to consider an assertion valid also when at least one element matches the condition; to do so, the ‘mode’ attribute will help:
|<assert-contains expression=”payload.items->color” value=”blue” mode=”one”/>|
At least one ‘color’ in the ‘items’ collection contains ‘blue’.