条件条件下的三元运算似乎不正确

问题描述 投票:0回答:1

这是此处

指出的问题的一部分

我在测试中更大规模地遇到了这些问题,然后在创建这个较小的可测试样本时再次遇到了这些问题

Feature:
  Scenario:
    * configure continueOnStepFailure = true

    * def sampleResponse =
    """
    {
      account: "123456",
      name: "sampletest",
      location: {
        locationId: "MyTown",
        latitude: "",
        longitude: "",
        timeZoneId: "America/Chicago"
      }
    }
    """
    * def location1 = sampleResponse.location
    * def sampleResponse2 =
    """
    {
      location: {
        locationId: "MyTown",
        timeZoneId: "America/Chicago"
      }
    }
    """
    * def location2 = sampleResponse2.location

    * print 'location1.latitude is:__',location1.latitude
    * print 'location2.latitude is:__',location2.latitude

    * match location1.latitude == location2.latitude
    * match location2.latitude == '#null'
    * match location2.latitude == '#string'
    * match location2.latitude == '#notpresent'

    * def loc2Value = location2.latitude == '#notpresent' ? 'missing' : 'valid'
    * print 'loc2Value is:__',loc2Value

文档中提到的条件(三元)运算符似乎是矛盾的,因为三元值应该是condition ? exprIfTrue : exprIfFalse

。尽管 
* def loc2Value = location2.latitude == '#notpresent' ? 'missing' : 'valid'
 通过了,但 
* match location2.latitude == '#notpresent'
 表明了这一点,结果是有效的。

karate
1个回答
0
投票
我会尽力澄清一件事。三元运算符是纯JS的。

match

#notpresent
之类的东西是空手道的特色。你不能混合两者。

我还将你的示例简化为更简单的内容:

* def response2 = { locationId: "MyTown", timeZoneId: "America/Chicago" } * match response2 contains { latitude: '#notpresent' } * def latitude = response2.latitude ? 'valid' : 'missing' * print 'latitude is:', latitude
最后我敦促您阅读此内容:

https://stackoverflow.com/a/54126724/143475 - 在我看来,您没有以正确的方式使用空手道,您似乎正在编写“过于聪明”的测试。这些更难编写(正如您肯定正在经历的那样),并且更难由其他团队维护。例如,我花了十五分钟才开始理解您原来的问题是什么,而且我没有时间查看您遇到同样问题的其他问题。

© www.soinside.com 2019 - 2024. All rights reserved.