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

时间:2019-04-11 22:41:50

标签: json parameter-passing spock

我正在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并没有采用param值。

我在这个测试中挣扎,有什么想法吗? 谢谢

2 个答案:

答案 0 :(得分:1)

以下代码也可以正常工作-不需要级联,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)

答案 1 :(得分:0)

我使用了字符串连接:

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)
    }