在 spock 测试中将参数传递给 json

Pass a param to a json in spock test

我正在 spock 测试中使用下一个 json,其中 podCastId 是一个动态值:

private buildPodCast(long podCastId) {
        String jsonString = '''
        {
          "id": ${podCastId},
          "lang": "en",
          "updated": "2019-04-03T19:48:29Z",
          "premium": false,
          "headline": "The Lowe Post",
          "description": "ESPN's Zach Lowe talks to various basketball people about various basketball things.",
          "thumbnails": {
            "light": {
              "href": "http://a.espncdn.com/i/espn/networks_shows/radio/crops/500/the_lowe_post.png",
              "width": 500,
              "height": 500
            }
          }
        }
        '''
        return JsonUtilKt.transformToJsonNode(jsonString)
    }

我的问题是我必须在 id 值中传递 podCastId 参数,但是按照现在的方式,json 没有采用参数值。

我正在为这个测试而苦恼,有什么想法吗? 谢谢

我使用了字符串连接:

private buildPodCast(long podCastId) {
        String jsonString = '''
        {
          "id": ''' + podCastId + ''',
          "lang": "en",
          "updated": "2019-04-03T19:48:29Z",
          "premium": false,
          "headline": "The Lowe Post",
          "description": "ESPN's Zach Lowe talks to various basketball people about various basketball things.",
          "thumbnails": {
            "light": {
              "href": "http://a.espncdn.com/i/espn/networks_shows/radio/crops/500/the_lowe_post.png",
              "width": 500,
              "height": 500
            }
          }
        }
        '''
        return JsonUtilKt.transformToJsonNode(jsonString)
    }

下面的代码也可以正常工作——不需要串联,string interpolation(适用于 """ 而不适用于 ''')完成了这项工作:

def buildPodCast(long podCastId) {
"""
{
    "id": ${podCastId},
    "lang": "en",
    "updated": "2019-04-03T19:48:29Z",
    "premium": false,
    "headline": "The Lowe Post",
    "description": "ESPN's Zach Lowe talks to various basketball people about various basketball things.",
    "thumbnails": {
        "light": {
            "href": "http://a.espncdn.com/i/espn/networks_shows/radio/crops/500/the_lowe_post.png",
            "width": 500,
            "height": 500
        }
    }
}
"""
}
buildPodCast(2)