使用定义的函数进行测试时出现 Postman AssertionError
我有一个 Postman 请求,收到了这样的 json 响应:
I have a Postman request getting a json response like so:
{
"Rules": [
{
"Type": "Melee",
"Captain": "Falcon",
"Falco": "Lombardi",
"Fox": "McCloud",
"Princess": "Peach",
"Kirby": null
},
{
"Type": "Brawl",
"Captain": "Toad",
"Falco": "The bird",
"Fox": "Blip",
"Princess": "Daisy",
"Kirby": null
},
{
"Type": "64",
"Captain": "America",
"Falco": "Dair",
"Fox": "Shine",
"Princess": "Float",
"Kirby": null
}
]
}
我想测试所有返回的值.问题是它不会总是按照这个顺序;例如,将来,可能会先发送64",然后是Brawl",然后是Melee"或类似的内容.所以我试图做一个循环来检查它是哪种类型,然后进行相应的测试:
I'd like to test all the values returned. The problem is that it won't always be in this order; for example, in the future, '64' might be sent first then 'Brawl' then 'Melee' or something along those lines. So I'm trying to make a loop that checks which type it is, then tests accordingly:
for(var i in jsonResponse.Rules)
{
if(jsonResponse.Rules[i] == "Melee")
{
pm.test("Melee Captain is Falcon", testFunction(jsonResponse.Rules[i].Captain, "Falcon");
pm.test("Melee Falco is Lombardi", testFunction(jsonResponse.Rules[i].Falco, "Lombardi");
//repeat for fox, princess and kirby
}
if(jsonResponse.Rules[i] == "Brawl")
{
pm.test("Brawl Captain is Toad", testFunction(jsonResponse.Rules[i].Captain, "Toad");
//repeat for the rest
}
if(jsonResponse.Rules[i] == "64")
{
pm.test("64 Captain is America", testFunction(jsonResponse.Rules[i].Captain, "America");
//repeat for the rest
}
}
这里是 testFunction 方法:
And here is the testFunction method:
function testFunction(value, shouldEqualThis)
{
pm.expect(value).to.eql(shouldEqualThis);
}
这将在测试通过时起作用,但如果测试失败,我会收到以下错误:
This will work when the test passes, but if the test fails I get the following error:
There was an error in evaluating the test script:
AssertionError: expected 'FalconSpelledWrong' to deeply equal 'Falcon'
每当我执行 'pm.test' 调用'testFunction' 的值不匹配时都是这种情况.
This is the case for anytime I do 'pm.test' that calls 'testFunction' with values that don't match.
我只想让测试失败而不是破坏脚本.
I just want the test to fail rather than break the script.
核心问题:我不明白这有什么区别:(工作)
pm.test("Melee Captain is Falcon", function() {
pm.expect(jsonResponseData.Rules[0].Captain).to.eql("FalconSpelledWrong");
})
这个:(不工作)
pm.test("Falcon equals FalconSpelledWrong", stringCompare("Falcon", "FalconSpelledWrong"));
function stringCompare(value, shouldEqualThis)
{
pm.expect(value).to.eql(shouldEqualThis);
}
第一个将无法通过测试并继续前进.第二个将抛出一个 AssertionError.
The first one will just fail the test and move on. The second will throw an AssertionError.
两者的区别是
首先你传递一个回调函数(抛出 AssertionFailure)而第二个你是显式调用函数(抛出 AssertionError)
With first you are passing a callback function (throws AssertionFailure) while with second you are explicitly calling the function (throws AssertionError)