我想确保每次发出特定的get请求时,我都会在名为spells
的正文中获得相同的结果。 (侧面注意:我还将编写一个不同的测试,以确保身体的另一部分active_spell
至少在某些时间是随机/不同的)。我已经编写了以下代码,但似乎无法进入数据'或者'结束'事件
那我为什么不进入数据'并且'结束'事件
真正的主要问题是,是否有更合适或更有效的方法来实现我的主要目标,即确保每次拼写本都返回相同的内容?
注意:我不介意查看图书馆来帮助解决这个问题,但我正在寻找一种方法来做到这一点,而无需添加更多内容。
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();
}
});
});
});
}
};