【问题标题】:Is there a simple match for objects containing array where the array content order doesn't matter?包含数组内容顺序无关紧要的数组的对象是否存在简单匹配?
【发布时间】:2021-01-28 22:25:35
【问题描述】:

当我不关心键的顺序或数组内容的顺序时,我试图比较包含数组的对象。包含深度接近我需要的,除了我需要双向使用它以确保数据中没有丢失的元素。理想情况下,我想要一个不区分顺序的== 版本。我下面有什么更好的选择吗?

    * def sample = { user: ['read', 'write', 'append'], public: ['read', 'append'] }
    * def test = { public: ['append', 'read'], user: ['write', 'read', 'append'] }
    * match sample contains deep test
    * match test contains deep sample

【问题讨论】:

    标签: karate


    【解决方案1】:

    使用 2 contains only。是的,我们还没有设计深度包含,它很少需要。

    * def sample = { user: ['read', 'write', 'append'], public: ['read', 'append'] }
    * match sample.public contains only ['append', 'read']
    * match sample.user contains only ['write', 'read', 'append']
    

    为了完整起见,尽管这可能有点矫枉过正,除非您想重用架构片段,但您可以这样做:

    * def user = ['write', 'read', 'append']
    * def public = ['append', 'read']
    
    * def sample = { user: ['read', 'write', 'append'], public: ['read', 'append'] }
    * match sample == { user: '#(^^user)', public: '#(^^public)' }
    

    【讨论】:

    • 谢谢 - 他们都是有用的想法。我想我正在突破空手道的界限,因为我专注于测试未知系统,所以我的所有匹配都需要更加灵活。我试图建立模式,使测试保持可读性并尽可能接近空手道语法。
    猜你喜欢
    • 2015-06-28
    • 2017-11-27
    • 2018-05-19
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    • 2018-05-01
    • 1970-01-01
    相关资源
    最近更新 更多