我是服务测试和Groovy的新手。以下是我的回复,
{
encodedDiscountId=1275479,
encodedGuid=gOHSkGzQEee4-AJiXJP2jg,
expirationDate=2017-08-17 17:00:00
}
我需要以下断言的帮助:
我尝试了脚本断言并继续收到此错误。
import groovy.json.JsonSlurper
def response = messageExchange.response.responseContent
def slurper = new JsonSlurper()
def json = slurper.parseText(response)
assert json.encodedDiscountId.size() == 7
assert json.encodedDiscountId.matches("[0-9]")
错误:
assert json.encodedDiscountId.matches("[0-9]") | | | | 1043947 false [encodedDiscountId:1043947, encodedGuid:l0wWcG2KEee4-AJiXJP2jg, expirationDate:2017-08-18 17:00:00]
答案 0 :(得分:1)
您的正则表达式仅适用于0-9中字符类中的单个字符。您需要声明整个字符串是数字,类似于/^[0-9]+$
。
^
匹配行的开头,$
匹配结尾,[0-9]+
表示至少1个数字。由于encodedDiscountId
看起来总是7位数,因此您可以将其作为^[0-9]{7}$
包含在正则表达式中。
由于正则表达式中有$
,如果您使用双引号,则需要将其作为\$
("^[0-9]+\$"
)转义或使用斜杠字符串(/^[0-9]+$/
)或单引号('^[0-9]+$'
)。