我在练习中有关于断言的问题。
我有这样的响应体:
[
{
"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
}
]
我写了test
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.model
是undefined
,因为response
是一个数组。
你可能忘了用model.model
代替response.model
pm.test("car model is Model 3", function () {
pm.expect(model.model).to.eql("Model 3");
});