检查JSON值是否具有与Postman中某些值匹配的字符串

发布于 2025-02-05 01:36:54 字数 3439 浏览 5 评论 0 原文

我正在尝试编写测试,以查看通过我的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.

I'm trying to write tests to see if a service is OK, Unknown, or Unreachable via my API. However, all of my tests come back as having passed. I know that all my servers are in an "OK" state, though, and that none of them are "Unreachable" or "Unknown." I believe the "unknown" and "unreachable" tests should be failing. What am I doing wrong?

Here's the body of the response:

{
    "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
    }
}

I've written the following tests:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弱骨蛰伏 2025-02-12 01:36:54
  • 您错过了一个级别来获得 dhcpservers 。它应该是 jsondata.result.dhcpservers ,而不是 jsondata.dhcpservers

  • 使用 .eql('ok'ok')而不是 >

测试将是:

pm.test('DHCP service is ok', () => {
    _.each(jsonData.result.dhcpServers, (item) => {
        pm.expect(item.state).eql('OK');
    })
})
  • You miss one level to get dhcpServers. It should be jsonData.result.dhcpServers, not jsonData.dhcpServers,

  • Use .eql('OK') instead of .to.have.body('OK')

The test would be:

pm.test('DHCP service is ok', () => {
    _.each(jsonData.result.dhcpServers, (item) => {
        pm.expect(item.state).eql('OK');
    })
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文