邮递员测试抛出“类型错误:无法读取未定义的属性‘get’”

Sun*_*sat 5 javascript postman

我如何使它工作?

目标:在测试脚本中调用一个变量

测试脚本:

pm.test("shipment registration not allowed", function () {
    var jsonData = pm.response.json();
 pm.expect(jsonData.results[0].errors.title).to.eql(pm.errors.get("shipmentRegistrationNotAllowed"));
});
Run Code Online (Sandbox Code Playgroud)

错误:

Error: TypeError: Cannot read property 'get' of undefined
Run Code Online (Sandbox Code Playgroud)

Dan*_*ton 4

我不确定您在哪里找到了pm.errors.get可以使用的功能,但我不相信它是 Postman 中的东西。

所有沙箱功能都可以在这里找到https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference

如果您只是想断言它jsonData.results[0].errors.title等于字符串值,您可以这样做:

pm.test("shipment registration not allowed", () => {
    var jsonData = pm.response.json()
    pm.expect(jsonData.results[0].errors.title).to.eql(pm.globals.get("my_error_value"))
})
Run Code Online (Sandbox Code Playgroud)

如果您设置一个global变量,您可以在测试中引用,如下所示:

在此输入图像描述