特定の get リクエストを行うたびに、 というボディ内の特定の配列に対して同じ結果が得られるようにしたいと考えていspells
ます。(補足: 体の別の部分active_spell
がランダム/異なることを少なくとも時々確認するために、別のテストも作成します)。次のコードを書きましたが、'data' または 'end' イベントに入らないようです。
では、なぜ「データ」イベントと「終了」イベントに入らないのでしょうか?
そして本当に重要な質問は、私の主な目的を達成するためのより適切または効率的な方法はありますか?
注:これを支援するためにライブラリを見てもかまいませんが、追加する必要なくこれを行う方法を探しています。
var config = require('./../config');
//Setup client with automatic tests on each response
var api = require('nodeunit-httpclient').create({
host: config.api.host,
port: config.api.port,
path: '/api', //Base URL for requests
status: 200, //Test each response is OK (can override later)
headers: { //Test that each response must have these headers (can override later)
'content-type': 'application/json'
}
});
exports.spellbook_get = {
'ensure spells are always the same': function(test) {
//2 default tests in the client plus the one below
test.expect(3);
var urls = ['/spellbook', '/spellbook', '/spellbook', '/spellbook'];
var completed_requests = 0;
urls.forEach(function(url) {
var responses = [];
api.get(test, url, function(res) {
res.on('data', function(chunk){
console.log('YAY!-DATA');
responses.push(chunk);
});
res.on('end', function(){
console.log('YAY!-END');
if (completed_requests++ == urls.length - 1) {
// All requests are completed
var uniques = [],
hashes = {};
for (var i=0; i < responses.length; i++) {
var hash = JSON.stringify(responses[i].spells);
if (!(hash in hashes)) {
hashes[hash] = true;
uniques.push(responses[i].spells);
}
}
//test that there is only 1 unique array
test.equal(uniques.length, 1, 'All spells responses must match');
test.done();
}
});
});
});
}
};