空手道中的比赛和打印有什么区别

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

match 与 print 的工作原理有何不同?

Feature: Create and verify User

  Background:
      * def createRandomUser =
      """
      function() {
        const random = Math.floor(Math.random() * 10000);
        const email = 'user' + random + '@example.com';
        const name = 'Name' + random;
        const surname = 'Surname' + random;
        return { email: email, name: name, surname: surname };
      }
      """

  Scenario: Create random user and verify
    Given def randomUser = createRandomUser()
    When def newUser = call read('CreateNewUser.feature') { randomUser: '#(randomUser)' }
    And match newUser.response.email == randomUser.email
    And match newUser.response.name == randomUser.name
    And match newUser.response.surname == randomUser.surname
    Then def usersList = call read('GetAllUsers.feature')
    And match usersList.response[-1].email == randomUser.email
    And match usersList.response[-1].name == randomUser.name
    And match usersList.response[-1].surname == randomUser.surname

在这个例子中,我有用户创建并将他添加到列表中,当我尝试打印

usersList.response[-1]
时,我收到null,但是当我将其与
randomUser.email
匹配时,它显示true,所以为什么打印显示null而匹配显示它有实际价值吗?然后我打印了
randomUser.email
,它显示了创建的电子邮件

我尝试打印

usersList.response[-1]
,而打印
usersList.response
显示响应中的所有值,还
usersList.response[usersList.response.length - 1]
显示正确的邮件不为空

karate
1个回答
0
投票

简短的回答,

print
在右侧采用“正常”JS,但
match
默认处理JSON路径

用圆括号括起来就可以使用JS。例如:

* def foo = [1, 2]
* print foo[foo.length - 1]
* match (foo[foo.length - 1]) == 2
© www.soinside.com 2019 - 2024. All rights reserved.