带有空手道 DSL 的嵌入式表达式不会替换 json 中的值

Embedded expressions with karate DSL don't replace value in json

所以基本上我只是从空手道测试框架开始,可能遗漏了一些非常简单的东西,但我似乎无法正确解析嵌入式表达式。如果我有一个像这样的功能文件,它可以通过几种方式做同样的事情:

Feature: Test Service

  Background:
    * url 'http://testurl:8080'
    * def localDateTime = Java.type('java.time.LocalDateTime')

  Scenario: Successful request

    * def createDateTime = LocalDateTime.now()
    * def testRequest =
    """
    {
      createDateTime: "#(createDateTime)",
      expiryDateTime:"#(localDateTime.now().plusMinutes(5))"
    }
    """
    * print testRequest
    * set testRequest.createDateTime = createdTime
    * print testRequest

然后当它到达打印行时,我得到这样的输出,其中值为空的 js 对象 {}

16:43:28.580 [main] INFO com.intuit.karate - [print] {"createDateTime":{},"expiryDateTime":{}}16:43:28.586 [main] DEBUG com.jayway.jsonpath.internal.PathCompiler - Using cached path: $true[]

另外,我可以看到它在哪里设置第一个打印语句的路径,如下所示:

16:32:30.612 [main] DEBUG com.jayway.jsonpath.internal.CompiledPath - Evaluating path: $['createDateTime'] 16:32:30.613 [main] DEBUG com.jayway.jsonpath.internal.JsonReader - Set path $['createDateTime'] new value 2017-09-14T16:32:30.566 16:32:30.629 [main] DEBUG com.jayway.jsonpath.internal.CompiledPath - Evaluating path: $['expiryDateTime'] 16:32:30.629 [main] DEBUG com.jayway.jsonpath.internal.JsonReader - Set path $['expiryDateTime'] new value 2017-09-14T16:37:30.621

任何人都可以向我解释为什么我无法将实际日期插入到 testRequest 中吗?

谢谢。

我认为如果将这些日期对象转换为字符串,您的问题就会得到解决。

* def LocalDateTime = Java.type('java.time.LocalDateTime')
* def createDate = LocalDateTime.now() + ''
* def expiryDate = LocalDateTime.now().plusMinutes(5) + ''
* def testRequest = { createDateTime: '#(createDate)', expiryDateTime: '#(expiryDate)' }
* print karate.pretty(testRequest)

以上对我有用,这是输出:

06:11:47.010 [main] INFO  com.intuit.karate - [print] {
  "createDateTime": "2017-09-15T06:11:46.983",
  "expiryDateTime": "2017-09-15T06:16:46.990"
}