2017-02-27 8 views
2

私はmochaテストフレームワークとchaiアサーションライブラリを使用してTypeScriptでいくつかのテストを書いています。エラーがスローされ、以下のコードでassert.fail()が呼び出されました。ただし、テストは合格とマークされます。私はなぜそれが起こるのか、私が約束に間違ったことをしているのか混乱しています。また、以下に示すように、UnhandledPromiseRejectionWarningがあります。私はキャッチブロックを含んでいたので、なぜこれが未処理のものとしてマークされているのかわかりません。私はこれを解決する方法についてのアドバイスを感謝します、ありがとう!Async Mochaテスト(Chaiアサーションライブラリを使用)は失敗しますが、合格とマークされます

User.all() 
     .then((users) => { 
     expect(users.length).to.equal(0); 
     return User.create('[email protected]', '12345', 'test', true, true); 
     }) 
     .then(() => { 
     return User.all(); 
     }) 
     .then((users) => { 
     expect(users.length).to.equal(1); 
     }) 
     .catch((error) => { 
     assert.fail(); 
     }); 

ベンジャミンGruenbaumが、これはそのブロックが約束を返すことを確認することによって固定された、上記に述べたように

✓ should create a new record 
    with existing e-mail in the database 
(node:29903) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): AssertionError: assert.fail() 
(node:29903) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 

Code

Debug Console

+2

'Users.all'コールの前に' return'を入れてください –

+1

ありがとう!私はちょうどそれを理解し、ポストしようとしていた、あなたは私にそれを打つ。私はこの記事(https://alisdair.mcdiarmid.org/simple-nodejs-tests-with-assert-and-mocha/)を読んでいました。「このテストを書くには、あなたのブロックが約束、そしてモカが残りの世話をする: – Lauren

答えて

4

デバッグコンソール

it('should create a new record',() => { 

    return User.all() 
     .then((users) => { 
     expect(users.length).to.equal(0); 
     return User.create('[email protected]', '12345', 'test', true, true); 
     }) 
     .then(() => { 
     return User.all(); 
     }) 
     .then((users) => { 
     expect(users.length).to.equal(1); 
     }) 
    .catch((error) => { 
     assert.fail(); 
    }); 
}); 
関連する問題