如何在POSTMAN工具中执行“或”操作

时间:2018-06-20 15:41:57

标签: javascript postman

我有一个测试来检查响应数组的对象是1个月,3个月还是6个月等。

我已经创建了下面的测试,但是这些测试一直在通过,即使输入错误也是如此。

public ChromeOptions getChromeEmulators(int width, int height) {
    Map<String, Object> deviceMetrics = new HashMap<String, Object>();
    Map<String, Object> mobileEmulation = new HashMap<String, Object>();
    ChromeOptions chromeOptions = new ChromeOptions();
    try {
        deviceMetrics.put("width", width);           
        deviceMetrics.put("height", height);
        deviceMetrics.put("pixelRatio", 3.0);
        mobileEmulation.put("deviceMetrics", deviceMetrics);
        mobileEmulation.put("userAgent",
"Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) 
AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile 
Safari/535.19");
       chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
    }catch (Exception e) {
        e.printStackStarce();
    }
    return chromeOptions;
}

和类似的东西

pm.test ("validate each object returns correct Frequency ", () => {
var fre = JSON.parse(responseBody);
for (i = 0; i < responseJson.length; i++){
    pm.expect(fre[i].FREQUENCY) === (('1 Month') || ('3 Months') || ('6 Months') || ('1 Year rolling') || ('Since Inception'));
} });

并且也尝试过这种方式

pm.test ("validate each object returns correct Frequency ", function(){
pm.expect(responseJson.every((fre) => {
    (fre.FREQUENCY) === ("1 Month") || ("3 Months") || ("6 Months") || ("1 Year rolling") || ("Since Inception");
})).to.be.true;
});

我的Json回复是这样的

pm.test ("validate each object returns correct Frequency ", function(){
pm.expect(responseJson.every((fre) => {
    pm.expect(fre.FREQUENCY).to.be.oneOf === ("1 Month"),("3 Months"),("6 Months"),("1 Year rolling"),("Since Inception");
})).to.be.true;});

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

您可以执行此操作,它已经接近您已经拥有的功能:

pm.test('Frequency Check', () => {
    _.each(pm.response.json(), (arrItem) => {
        pm.expect(arrItem.FREQUENCY).to.be.oneOf(["1 Month" , "3 Months" , "6 Months" , "1 Year rolling", "Since Inception"])
    })
})

我稍微修改了响应数据,因此您可以看到失败时的样子:

Postman