org.spockframework:spock-core:0.7-groovy-2.0
Gradle 1.12
Groovy 1.8.6
你好,
我有一个将返回 JSON 字符串的函数。我想测试我的函数是否正确创建 JSON 格式。但是,我可能遇到一些转义字符的问题,因为测试总是失败。
这是我期望并测试的正确 JSON 格式:
{"function":"verifyEmail","parameters":[{"email_pwd":"password","session_id":"S1234","snapz_id":"T1234","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8beacbe3e4ffe6eae2e7a5e8e4e6" rel="noreferrer noopener nofollow">[email protected]</a>","access_token":"abcd"}]}
但是,由于存在双引号
和[ ]
字符,因此设置条件很困难。我曾试图逃离他们。
这是函数:
def 'Parse the string to create JSON object'() {
setup:
def requestFactory = new RequestFactory();
def snapzJSON = requestFactory.createWSRequest(FunctionReq.VERIFY_EMAIL, accessToken, sessionId, snapzId, email, emailPwd)
expect: 'Correctly formats JSON string'
System.out.println("SPOCK TEST: " + snapzJSON.createJSONRequest())
snapzJSON.createJSONRequest() == '{\"function\":\"verifyEmail\",\"parameters\":\\[{\"email_pwd\":\"password\",\"session_id\":\"S1234\",\"snapz_id\":\"T1234\",\"email\":\"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c5a485adaab1a8a4aca9eba6aaa8" rel="noreferrer noopener nofollow">[email protected]</a>\",\"access_token\":\"abcd\"}\\]}'
}
这就是结果:
Condition not satisfied:
snapzJSON.createJSONRequest() == '{\"function\":\"verifyEmail\",\"parameters\":\\[{\"email_pwd\":\"password\",\"session_id\":\"S1234\",\"snapz_id\":\"T1234\",\"email\":\"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e687a68e89928b878f8ac885898b" rel="noreferrer noopener nofollow">[email protected]</a>\",\"access_token\":\"abcd\"}\\]}'
| | |
| | false
| [parameters:[[session_id:S1234, snapz_id:T1234, email:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bddcfdd5d2c9d0dcd4d193ded2d0" rel="noreferrer noopener nofollow">[email protected]</a>, email_pwd:password, access_token:abcd]], function:verifyEmail]
abcd S1234 T1234 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cbaa8ba3a4bfa6aaa2a7e5a8a4a6" rel="noreferrer noopener nofollow">[email protected]</a> password
有更简单的方法吗?
非常感谢您的建议,
测试结果的一些额外输出:
jsonObject: {"parameters":[{"session_id":"S1234","snapz_id":"T1234","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbdafbd3d4cfd6dad2d795d8d4d6" rel="noreferrer noopener nofollow">[email protected]</a>","email_pwd":"password","access_token":"abcd"}],"function":"verifyEmail"}
SPOCK TEST: [parameters:[[session_id:S1234, snapz_id:T1234, email:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="38597850574c55595154165b5755" rel="noreferrer noopener nofollow">[email protected]</a>, email_pwd:password, access_token:abcd]], function:verifyEmail]
最佳答案
在这一行中:
snapzJSON.createJSONRequest() == '{\"function\":\"verifyEmail\",\"parameters\":\\[{\"email_pwd\":\"password\",\"session_id\":\"S1234\",\"snapz_id\":\"T1234\",\"email\":\"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d3c1d353229303c3431733e3230" rel="noreferrer noopener nofollow">[email protected]</a>\",\"access_token\":\"abcd\"}\\]}'
您正在将String
与Map
进行比较。您需要解析右侧的 JSON 字符串(使用 JsonSlurper)并比较值。当使用单引号 '
时,也无需转义 "
。
您还可以使用 JsonOutput
将左侧(snapzJSON.createJSONRequest()
)序列化为字符串,然后进行比较。
比较 map 要好得多。
关于groovy - 使用spock测试复杂的JSON字符串条件永远不满足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24628415/