gpt4 book ai didi

javascript - 使用异步命令 Chai 断言不会失败?

转载 作者:行者123 更新时间:2023-12-03 00:08:53 29 4
gpt4 key购买 nike

使用以下异步命令时,我的 Chai 断言似乎没有失败:

async confirmSuccessfulSubmission() {
try {
let value = await $("#contact_reply h1").getText()
if(value)
return expect(value).to.equal("Thank You for your Message!2");
return null
} catch(e) {
console.log(e)
}
}

异常消息输出到控制台窗口:

[0-0] { AssertionError: expected 'Thank You for your Message!' to equal 'Thank You for your Message!2'
at ContactUs_Page.confirmSuccessfulSubmission (C:\Users\GBruno\Desktop\webdriverioFramework\pageObjects\ContactUs_Page.js:51:34)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
message: 'expected \'Thank You for your Message!\' to equal \'Thank You for your Message!2\'',
showDiff: true,
actual: 'Thank You for your Message!',
expected: 'Thank You for your Message!2' }

最佳答案

为了让你的测试失败,测试运行程序(我假设它是 webdriverio)必须收到一个 AssertionError。在您的测试中,chai 将在这一行中抛出错误:

expect(value).to.equal("Thank You for your Message!2");

但你在这里明白了:

catch(e) {
console.log(e)
}

因此它永远不会到达测试运行程序,测试也不会失败。请尝试使用以下代码:

async confirmSuccessfulSubmission() {
try {
let value = await $("#contact_reply h1").getText()
expect(value).to.equal("Thank You for your Message!2");
} catch(e) {
console.log(e)
throw(e)
}
}

或者如果您不需要打印错误:

async confirmSuccessfulSubmission() {
let value = await $("#contact_reply h1").getText()
expect(value).to.equal("Thank You for your Message!2");
}

关于javascript - 使用异步命令 Chai 断言不会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54843673/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com