如何在空手道中直接匹配数据库查询结果和API响应

时间:2018-06-21 10:14:47

标签: karate intuit

我的代码如下=> @UserValidationEmail

Feature: Verify User Details 

  Scenario: Verify User Details api

    Given url 'http://127.0.0.1/user/?id=123'
    When method get
    Then status 200

    * def DbUtils = Java.type('com.intuit.karate.treebo.util.DbUtils')
    * def db = new DbUtils()

    * def query = "SELECT * FROM user_table where id = 123"
    * def dbResult = db.readRows(query)


    * match dbResult[0] contains response.data

API调用http://127.0.0.1/user/?id=123的响应为=>

{"status":"success","data":{"id":123,"created_at":"2018-01-18T10:27:24.631959","modified_at":"2018-01 18T10:27:24.641493","first_name":"Foo","last_name":"Bar","email":"foo@bar.com","phone_number":null}}

查询在数据库上的响应是=>

[print] dbResult: [{id=123, password=bZeGU3h66cp, created_at=2018-01-18 15:57:24.631959, modified_at=2018-01-18 15:57:24.641493, first_name=Foo, last_name=Bar, email=foo@bar.com, phone_number=null}]

在尝试使用contains将上述两个结果匹配时,出现以下错误:

Tests in error: 
  * match dbResult[0] contains response.data(Scenario: Verify User Details api): path: $[0].created_at, actual: 2018-01-18 15:57:24.631959, expected: '2018-01-18T10:27:24.631959', reason: actual value is not a string

我如何将数据库查询的结果直接与API响应匹配?

1 个答案:

答案 0 :(得分:1)

让我们看看消息:

path: $[0].created_at, actual: 2018-01-18 15:57:24.631959, expected: '2018-01-18T10:27:24.631959'
reason: actual value is not a string

很显然,返回的结果行具有java.util.Date或类似名称,而不是字符串。即使它是字符串,也请注意,它们的值是不同的。

要么将该字段转换为字符串,然后进行比较。或最有可能为您工作的方法-只需在比较之前进行此操作即可:

* set response.data.created_at = '#ignore'

如果您需要了解更多信息,请阅读有关模糊匹配的文档:https://github.com/intuit/karate#fuzzy-matching