【问题标题】:Continuate test after first exception | Chai | Mocha第一次异常后继续测试 |柴 |摩卡
【发布时间】:2017-12-14 16:29:09
【问题描述】:

我正在使用 http 请求编写单元测试。 我想测试不同的字段作为响应,但是当我检查第一个异常 CHAI 中的字段时,CHAI 关闭了所有,单次测试也是如此。我希望 CHAI 继续执行他的测试。 我该怎么做?

这是一个如何使用expect的例子。

expect(res.body.data.user_data,"last_name").have.property('last_name'); expect(res.body.data.user_data.last_name,"last_name").be.a('number'); expect(res.body.data.user_data,"username").have.property('username'); expect(res.body.data.user_data.username,"username").be.a('string');

【问题讨论】:

  • 如果你把它放在try... catch 块里呢?
  • 你可以把期望放在单独的测试中

标签: javascript node.js mocha.js chai chai-http


【解决方案1】:

如果您确实知道某个特定语句会抛出exception,您可以使用以下方式对其进行测试。

expect(res.body.data.user_data).to.throw('Oh no')

或者如果你想检查和处理你得到的异常,你可以使用trycatch块,如下所示:

try {
    expect(res.body.data.user_data, "last_name").have.property('last_name');
    expect(res.body.data.user_data.last_name, "last_name").be.a('number');
    expect(res.body.data.user_data, "username").have.property('username');
    expect(res.body.data.user_data.username, "username").be.a('string');
}
catch (e) {
    //write err and res objects to custom log file
    throw e;
}

我认为即使在Chai 中的断言失败之后也无法继续。

【讨论】:

    猜你喜欢
    • 2018-05-24
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 2023-04-11
    • 2021-02-15
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    相关资源
    最近更新 更多