2016-10-11 7 views
0

私はNode.jsとExpressでAPIを開発しています。私はユニットテストを書くためにMochaとSupertestを使っています。私は、私のエラー処理がうまくいっているかどうかを見るために、ほぼ無作為のパラメータですべてのルートをテストするテストの大きなファイルを持っています。タイムアウトが超過したのはなぜですか?

私の要求がタイムアウトし始めたまで、すべてが素晴らしかったです。

これは、多かれ少なかれ私のコードです:

var supertest = require("supertest"); 
var should = require("should"); 

var server = supertest.agent("http://localhost:3000"); 

function requestAuth(url, type, auth, params, callback) { 
    if (params == null) { 
    server[type](url) 
    .type('form') 
    .auth(auth.email, auth.password) 
    .expect("Content-type",/json/) 
    .expect(200) 
    .end(callback); 
    } 
    else { 
    server[type](url) 
    .send(params) 
    .auth(auth.email, auth.password) 
    .type('form') 
    .expect("Content-type",/json/) 
    .expect(200) 
    .end(callback); 
    } 
} 

describe('Testing route 1', function() { 
    describe('Testing param 1 error handling', function() { 
    it('should return error 1', function(done) { 
     requestAuth(route1, "post", {email: email, password: password}, {param1: "blahblahblah"}, 
     function(err, res) { 
     res.body.should.have.property('error'); 
     done(); 
     }); 
    }); 
    it('should return error 2', function(done) { 
     requestAuth(route1, "post", {email: email, password: password}, {param1: "blahblahblah"}, 
     function(err, res) { 
     res.body.should.have.property('error'); 
     done(); 
     }); 
    }); 
    // etc 
    }); 
    describe('Testing param 2 error handling', function() { 
    it('should return error 3', function(done) { 
     requestAuth(route1, "post", {email: email, password: password}, {param1: "blahblahblah"}, 
     function(err, res) { 
     res.body.should.have.property('error'); 
     done(); 
     }); 
    }); 
    it('should return error 4', function(done) { 
     requestAuth(route1, "post", {email: email, password: password}, {param1: "blahblahblah"}, 
     function(err, res) { 
     res.body.should.have.property('error'); 
     done(); 
     }); 
    }); 
    // etc 
    }); 
    //etc 
}); 

describe('Testing route 2', function() { 
    //etc 
}); 

私は多くのテストを持っていることを除いて。 は、ある時点で、私はルート8をテストしていたときに、すべてのテストは、次のメッセージで失敗し始めていることとしましょう:

12) Route 8 Testing Param 1 error handling should return error 1: 
    Error: timeout of 2000ms exceeded 
     at null.<anonymous> (/usr/lib/nodejs/mocha/lib/runnable.js:139:19) 
     at Timer.listOnTimeout (timers.js:92:15) 

私は本当にそれを取得しないでください。それ以来、すべてがうまくいった、すべての要求の最後に行われた、良いことがあります。ルートは問題ありませんが、サーバ側では何も起こりません。これは本当に変です...

また、もしルート8のテストが奇妙になり、私がルート7のテストをコメントすると、ルート9のテストは間違った動作を開始します。

これはsupertestから来ていると思います。それは過負荷になっている可能性がありますか?どうすればそれを修正できますか?

ご回答いただきありがとうございます。

答えて

1

あなた自身のテストには、あなたの行動を完了するための時間制限があります。これは、リソースが設定されておらず、その2秒間に利用可能でない場合、または2秒後にテストが完了した場合、失敗することを意味します。失敗したテストの最初の行にthis.timeout = [milliseconds]を使用して、タイムアウトを延長します。

MochaJS Test Level Timeouts

+0

ええ、私はこれを知っています。私の要求は2秒以上かかることはなく、APIに到達することさえできません。 – Devz

+0

大規模なスイートごとに、サーバー初期化をbeforeステートメントに移そうとしましたか?これは、非同期呼び出しが期待どおりに動作し、すべての要求が完了する前にサーバーが切断されているため、奇妙な原因となる可能性があります。 – gelliott181

+0

これは、テストの数に依存しているので、解くのは少し難しいです。これは非同期の問題のように聞こえ、Expressは実行していない場合を除き、確実​​に応答しなくてはなりません。テストが失敗する前に – gelliott181

関連する問題