我正在使用 http 请求编写单元测试。 我想测试不同的字段作为响应,但是当我检查第一个异常中的字段时,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,"用户名").have.property('用户名');
Expect(res.body.data.user_data.username,"用户名").be.a('string');
最佳答案
如果您确实知道特定语句会引发异常
,您可以使用以下方法来测试它。
expect(res.body.data.user_data).to.throw('Oh no')
或者,如果您想检查并处理遇到的异常,可以使用 try
和 catch
block ,如下所示:
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
中断言失败后也不可能继续。
关于javascript - 第一次异常后继续测试 | Chai | Mocha ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47818060/