检查JSON值是否具有与Postman中某些值匹配的字符串
我正在尝试编写测试,以查看通过我的API的服务是否还可以,未知或无法实现。但是,我所有的测试都回来了。我知道我所有的服务器都处于“确定”状态,并且它们都不是“无法到达”或“未知”。我相信“未知”和“无法到达”的测试应该失败。我在做什么错?
这是响应的主体:
{
"result": {
"dhcpServers": [
{
"ref": "DHCPServers/4",
"name": "unixdns",
"address": "10.9.0.220",
"resolvedAddress": "10.9.0.220",
"username": "",
"password": "",
"type": "ISC",
"state": "OK",
"security": "Unknown",
"customProperties": {},
"enabled": true,
"dhcpv6": false
},
{
"ref": "DHCPServers/15",
"name": "mmappliance",
"address": "10.9.0.156",
"resolvedAddress": "10.9.0.156",
"username": "",
"password": "",
"type": "ISC",
"state": "OK",
"security": "Unknown",
"customProperties": {},
"enabled": true,
"dhcpv6": false
},
{
"ref": "DHCPServers/19",
"name": "WIN-51",
"proxy": "10.9.0.150",
"address": "10.9.0.150",
"resolvedAddress": "10.9.0.150",
"username": "",
"password": "",
"type": "MSDHCP",
"state": "OK",
"security": "Unknown",
"customProperties": {},
"enabled": true,
"dhcpv6": true
},
{
"ref": "DHCPServers/22",
"name": "WIN-PM",
"proxy": "10.9.0.100",
"address": "10.9.0.100",
"resolvedAddress": "10.9.0.100",
"username": "",
"password": "",
"type": "MSDHCP",
"state": "OK",
"security": "Unknown",
"customProperties": {},
"enabled": true,
"dhcpv6": true
}
],
"totalResults": 4
}
}
我已经编写了以下测试:
let jsonData = pm.response.json()
pm.test('DHCP service is ok', () => {
_.each(jsonData.dhcpServers, (item) => {
pm.expect(item.state).to.have.body('OK')
})
})
pm.test('DHCP service is Unknown', () => {
_.each(jsonData.dhcpServers, (item) => {
pm.expect(item.state).to.have.body('Unknown')
})
})
pm.test('DHCP service is unreachable', () => {
_.each(jsonData.dhcpServers, (item) => {
pm.expect(item.state).to.have.body('Unreachable')
})
})
//Unknown - DHCP Server Controller status is unknown.
//OK - DHCP Server Controller and service are both OK.
//Unreachable - DHCP Server Controller is offline or otherwise unreachable.
//Out of date - DHCP Server Controller has a different version than Central.
//Updating - DHCP Server Controller version is being updated.
//Uninitialized - DHCP Server is on a uninitialized appliance that needs to be manually initialized.
//Detached - DHCP Server has been detached without removing it from the system.
//DHCP Service Down - DHCP Server service is down and DNS server is not responding to queries.
//DHCP Service Impaired - DHCP Server service is running but impaired.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您错过了一个级别来获得
dhcpservers
。它应该是jsondata.result.dhcpservers
,而不是jsondata.dhcpservers
,使用
.eql('ok'ok')
而不是>
测试将是:
You miss one level to get
dhcpServers
. It should bejsonData.result.dhcpServers
, notjsonData.dhcpServers
,Use
.eql('OK')
instead of.to.have.body('OK')
The test would be: