Are #ignore and #present the same thing?

假装没事ソ 提交于 2021-02-05 05:52:31

问题


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"}

回答1:


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' }



回答2:


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' }


来源:https://stackoverflow.com/questions/53871983/are-ignore-and-present-the-same-thing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!