如何使用空手道验证非JSON响应主体(2)

时间:2019-08-13 07:00:46

标签: karate

这是How to validate Non-JSON response body using Karate的延续。

详细信息:进行API后调用时,如果数据库中已有该雇员,则将在响应正文中按以下方式抛出错误响应,该响应不是Json / String格式:

{"error":{"text":SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'NewEmp' for key 'employee_name_unique'}}

我的目的是验证是否可以按预期方式抛出错误响应。

我尝试了How to validate Non-JSON response body using Karate中提供的解决方案,但是该解决方案无法正常工作。详细信息如下:

我不理解如何使用上一个问题的解决方案中提供的*。您能解释一下如何使用*

空手道特征:

Scenario: Testing non-string response
Given url 'dummy.restapiexample.com/api/v1/create' 
And request {"name":"PutTest8","salary":"123","age":"23"} 
When method POST 
Then status 200 
* string temp = response 
And match temp contains 'error'

上面抛出了如下错误-

line 20:4 mismatched input '*' expecting <EOF>

17:43:46.230 [main] ERROR com.intuit.karate.core.FeatureParser - syntax error: mismatched input '*' expecting <EOF>

17:43:46.235 [main] ERROR com.intuit.karate.core.FeatureParser - not a valid feature file: src/test/java/learnKarate/postcall.feature - mismatched input '*' expecting <EOF>

注意:我还尝试“声明”响应-同样失败,并出现以下错误。

Then assert $ contains 'error'

错误:

com.intuit.karate.exception.KarateException: postcall.feature:29 - javascript evaluation failed: $ contains 'error', <eval>:1:2 Expected ; but found contains
$ contains 'error'
  ^ in <eval> at line number 1 at column number 2
    at ✽.Then assert $ contains 'error' (postcall.feature:29)

1 个答案:

答案 0 :(得分:0)

您的示例或环境中存在严重错误。 *只是Given When等的替代。例如,将其粘贴到新的Scenario中,这对我有用:

* def response = 'error'
* string temp = response 
And match temp contains 'error'

由于您似乎被困住了,现在该您执行以下过程了:https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

祝一切顺利!