2017-08-23 1 views
0

すべきテスト:モカチャイがPass buthがここに現在のテストだではない

describe('/POST Register Page', function() { 
    it('it should register new user', function(/*done*/) { 
     chai.request(server) 
     .post('/auth/register') 
     .send(new_user_data) 
     .end(function(res) { 
      expect(res).to.have.status(2017); 
      // done(); 
     }) 
    }) 
    }) 

私が最後にチェック、2017など一切HTTPコードがありません、しかし、それはまだ渡し:

Registration 
    Get register page 
GET /auth/register 200 6.989 ms - 27 
     ✓ it should get register page 
    /POST Register Page 
     ✓ it should register new user 


    2 passing (147ms) 

私が欲しいです単純に何かを投稿してから返答を返し、返答で遊ぶことができます。私は何かが間違っているか、右、テストに合格するかどうかについては、仕事を得ることができない

1) Registration /POST Register Page it should register new user: 
    Error: Timeout of 3000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. 

:私はdone()が含まれている場合

は、私は、タイムアウトエラーが発生します。

予想通り、この取得要求が通過しているものの:

describe('Get register page', function() { 
    it('it should get register page', function(done) { 
     chai.request(server) 
     .get('/auth/register') 
     .end(function(err, res) { 
      expect(err).to.be.null; 
      expect(res).to.have.status(200); 
      done(); 
     }) 
    }) 
    }) 

を、私はこのモカ兼チャイ・チャイ-HTTPの事で新たなんだ、と経験は、これまでに奇妙です。

ありがとうございました。

+0

あなたは 'done'コールバックを使用しないように計画している場合(あなたが約束を持っている場合、あなたはいけません)そしてちょうど' chai.request'を返します。 [Promise]機能(https://github.com/chaijs/chai-http#dealing-with-the-response---promises)が有効になっていることを確認してください。 – MarcoL

答えて

1

POSTリクエストが完了するまでに3秒以上かかることがあるため、mochaはタイムアウトエラーをスローします。

あなたは次のように大きな値にタイムアウトを設定してみてくださいすることができますいくつかのトライアルを

describe('/POST Register Page', function() { 
    // timeout in milliseconds 
    this.timeout(15000); 

    // test case 
    it('it should register new user', function(done) { 
    chai.request(server) 
     .post('/auth/register') 
     .send(new_user_data) 
     .end(function(res) { 
     expect(res).to.have.status(200); 
     done(); 
     }) 
    }) 
}) 

は、あなたのテストで設定したタイムアウトの最適値を把握することができます。

done()コールバックを使用しない場合、mochaは実際の応答が到着するのを待たずにアサーションをスキップします。 .end()ブロックのアサーションは決して実行されないので、mochaはアサーションがないのでテストをパスします。 TDDで始めたときに私は似たようなことに直面していました.TDDは難しいことを学びました。

Reference

Because the end function is passed a callback, assertions are run asynchronously. Therefore, a mechanism must be used to notify the testing framework that the callback has completed. Otherwise, the test will pass before the assertions are checked.

関連する問題