当我们使用assert inside promise时,测试用例没有失败

问题描述 投票:0回答:1

以下是我的代码。无论我的实际是否等于预期或不等于测试用例,它只是显示警告消息,当它不相等但我的测试用例显示为绿色。我无法在报告中看到任何失败消息。

码:

this.VerifyAccountHolder=async function(){
 var testPromise = new Promise(function(resolve, reject) {
    setTimeout(function() {
          resolve(elementAccount.isPresent());
    }, 200);

});
var result = await testPromise;
assert.equal(result,true,"wrong result");

警告信息:

(node:8784)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):AssertionError:预期false等于true(节点:8784)[DEP0018]弃用警告:不推荐使用未处理的承诺拒绝。将来,未处理的承诺拒绝将使用非零退出代码终止Nodejs进程。

如何避免这种警告

我想在report中显示断言错误。我是JavaScript量角器框架的新手。

javascript protractor
1个回答
0
投票

您可以通过完全删除承诺来简化整个功能。我真的不确定你为什么需要setTimeout。该元素将呈现或不存在。你可以使用ExpectedConditions.presenceOf()browser.wait(),但这对我来说似乎是多余的。尽管我讨厌建议这个,如果你真的必须等待200ms才能检查元素是否存在只需使用browser.sleep()。我会用它作为最后的手段。如果可以的话,尝试让它工作而不需要任何明确的等待。

this.VerifyAccountHolder = function() {
    browser.sleep(200); //only use this if you REALLY need it
    return elementAccount.isPresent();    
});

然后你应该将断言移到测试而不是将它保留在这个函数中。

it('should be present', async () => {
    let result = await pageObjectName.VerifyAccountHolder();
    expect(result).toEqual(true);

    //or if you prefer to keep using a node assertion instead
    //assert.equal(result,true,"element not present");
});
© www.soinside.com 2019 - 2024. All rights reserved.