2017-12-14 4 views
0

私のユニットテストはhttpリクエストで書いています。 私は応答で異なるフィールドをテストしたいが、最初の例外CHAIでフィールドをチェックすると、すべての単一のテストも閉じます。私はチャイが彼のテストを続けて欲しい。 どうすればいいですか?最初の例外の後で継続的なテスト|チャイ| Mocha

これは、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');

+1

'try ... catch'ブロックに入れるとどうなりますか? –

+0

あなたは別々のテストで期待を置くことができます – Robbie

答えて

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の失敗。

関連する問題