简体   繁体   English

第一次例外后继续测试| 柴| 摩卡

[英]Continuate test after first exception | Chai | Mocha

I'm writing my unit test with an http request. 我正在用http请求编写单元测试。 I want to test different fields in response but when I check fields in the first exception CHAI closes all, single test too. 我想测试不同的字段以作为响应,但是当我在第一个异常中检查字段时,CHAI也会关闭所有的单个测试。 I want that CHAI continues his test execution. 我希望CHAI继续执行他的测试。 How can I do this? 我怎样才能做到这一点?

This is an a example how to use the 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');

If you surely knows that a particular statement will throws an exception you can use following way to test it. 如果您确实知道特定的语句将引发exception ,则可以使用以下方式对其进行测试。

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

Or if you want to check and handle the exception you are getting you can use try and catch block as follows: 或者,如果要检查和处理异常,则可以使用try and catch块,如下所示:

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;
}

I don't think its possible to continue even after the assertion failure in Chai . 我认为即使在Chai断言失败之后,这种可能性也不会继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM