【问题标题】:async function in mochamocha 中的异步函数
【发布时间】:2019-08-08 19:53:59
【问题描述】:

在尝试为测试用例断言错误消息时收到问题

AssertionError: expected [Function] to throw an error
at Context.it (test\index.js:24:80)
if (configVms.rts) {
describe('Real Time Services', () => {
    for(let rtsConfig of configVms["rts"].rtsConfig) {
        it(`Real Time Services endpoints for guid ${rtsConfig.resourceId} on ${rtsConfig.platformType} platform`, async () => {
            expect(async () => await realTimeServices.main(rtsConfig)).to.throw('Host rejected');
        });
    }});
}

【问题讨论】:

  • 试试expect(await realTimeServices.main(rtsConfig)).to.throw('Host denied');

标签: javascript asynchronous mocha.js assert


【解决方案1】:

to.throw 仅适用于同步函数。 在您的情况下,我建议添加 chai-as-promised 插件并更改测试:

if (configVms.rts) {
describe('Real Time Services', () => {
    for(let rtsConfig of configVms["rts"].rtsConfig) {
        it(`Real Time Services endpoints for guid ${rtsConfig.resourceId} on ${rtsConfig.platformType} platform`, async () => {
            await expect(realTimeServices.main(rtsConfig)).to.be.rejectedWith('Host rejected');
        });
    }});
}

【讨论】:

    猜你喜欢
    • 2014-02-08
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    相关资源
    最近更新 更多