Are #ignore and #present the same thing?

Viewed 973

What's a case where I'd want to use #ignore vs. #present or vice-versa? Or are they identical?

My first read of the docs I thought the following would pass, but it does not. Edit to add: This fails in Karate 0.9.0 but passes in 0.8.0.

* def foo = {a: 1}
* match foo == {a: 1, b: "#ignore"}

These do pass:

* def foo = {a: 1}
* match foo == {a: 1, b: "##ignore"}
* match foo == {a: 1, b: "##present"}
2 Answers

Yes, when you want to match for the key not being present or null, use the double-hash:

* def foo = { a: 1 }
* match foo == { a: 1, b: '##ignore' }
* match foo == { a: 1, b: '#notpresent' }

* def foo = { a: 1, b: null }
* match foo == { a: 1, b: '##ignore' }
* match foo == { a: 1, b: '#present' }

* def foo = { a: 1, b: 'bar' }
* match foo == { a: 1, b: '##ignore' }
* match foo == { a: 1, b: '#present' }

No, #ignore and #present are not intended to have the same behavior.

There was a bug in Karate 0.9.0 that caused them to behave the same, but that is fixed.

#ignore should match whether or not the key is present.

# This case would NOT match with '#present'
# This case fails in Karate 0.9.0 due to a bug
* def foo = {}
* match foo == { a: '#ignore' }

# These cases would also match with '#present'
* def foo = {a: null}
* match foo == { a: '#ignore' }
* def foo = {a: "bar"}
* match foo == { a: '#ignore' }
Related