邮递员断言

Postman Assertion

我在练习过程中遇到了与断言有关的问题。

我的响应正文如下:

[
    {
        "id": 1,
        "manufacturer": "Ford",
        "model": "Model T",
        "build": 1927
    },
    {
        "id": 2,
        "manufacturer": "Tesla",
        "model": "Model 3",
        "build": 2017
    },
    {
        "id": 3,
        "manufacturer": "Tesla",
        "model": "Cybertruck",
        "build": 2019
    }
]

我写了测试

const response = pm.response.json()
 
let model;
 
for (let filter of response) {
    if (filter.model === "Model 3") {
       //console.log(filter)
       model = filter;
    }
}
console.log(model)
 
pm.test("car model is Model 3", function () {
    pm.expect(response.model).to.eql("Model 3");
});

不幸的是,我的测试失败了 AssertionError: expected undefined to deeply equal 'Model 3' 我试图弄清楚但不知道为什么这没有通过,因为我对该模型的导航是正确的。你能指导我我的代码有什么问题吗?

response.modelundefined 因为 response 是一个数组。
您可能忘记使用 model.model 而不是 response.model

pm.test("car model is Model 3", function () {
    pm.expect(model.model).to.eql("Model 3");
});