尝试在节点中围绕TCP服务器并在Mocha中进行测试!
这是我到目前为止所做的:
在tcp-server.js
中'use strict';
var net = require('net');
var fs = require('fs');
var server = module.exports.server = net.createServer(function(socket){
socket.on('data', function(data){
console.log(data.toString());
var requestString = "Requested on: " + new Date().toString() + "\n" + data.toString();
var writeStream = fs.createWriteStream("datalog.log", {'flags': 'a'});
writeStream.write(requestString);
writeStream.end();
});
socket.on('end', function(){
console.log('socket closed');
});
});
server.listen('3000', function(){
console.log('server up');
});
在test.js中:
var fs = require('fs');
var net = require('net');
var expect = require('chai').expect;
var server = require(__dirname + "/../tcp-server.js").server;
var request = require('superagent');
describe('the tcp server', function() {
beforeEach(function(){
server.listen('3000', function(){
console.log('server up in mocha!');
});
});
it('should log all requests to a log file', function(done){
request.get('http://localhost:3000').end(function(err, res){
expect(true).to.equal(false); //This test doesn't run
//eventually write some fs code that tests if the log file is written to;
done();
});
});
});
看起来tcp-server.js中的console.log(data.toString())行会触发,但console.log不会更新。当mocha超时(即没有done()调用)时,日志似乎只会更新。
我有什么想法可以确保在'data'事件之后执行的所有操作都能执行,以便我可以检查日志是否已更新?
谢谢!
编辑:问题不在于测试,而在于TCP服务器(我没有正确关闭它)。
在tcp-server.js
中'use strict';
var net = require('net');
var fs = require('fs');
var server = module.exports.server = net.createServer(function(socket){
socket.on('data', function(data){
var requestString = data.toString();
var writeStream = fs.createWriteStream("datalog.log", {'flags': 'a'});
writeStream.write(requestString);
writeStream.end();
socket.end();
});
socket.on('end', function(){
console.log('socket closed');
server.close();
});
});
server.listen('3000', function(){
console.log('server up');
});
var client = net.connect({port: 3000},
function() { //'connect' listener
console.log('connected to server!');
client.write("Requested on: " + new Date().toString() + "\n");
});
在test.js
var fs = require('fs');
var net = require('net');
var expect = require('chai').expect;
var server = require(__dirname + "/../tcp-server.js").server;
describe('the tcp server', function() {
var logstringBefore = "";
var logstringAfter = "";
before(function(done){
fs.readFile('datalog.log', function(err, data){
logstringBefore = data.toString();
done();
});
});
it('should log all requests to a log file and change it', function(done){
server.listen('3000', function(){
var requestString = "Requested on: " + new Date().toString();
console.log('server up in mocha!');
fs.readFile('datalog.log', function(err, data){
logstringAfter = data.toString();
expect(logstringAfter).to.include(requestString);
expect(logstringBefore).to.not.equal(logstringAfter);
done();
});
});
});
});
答案 0 :(得分:1)
done()
执行完毕后调用{p> end()
,即如下所示触发回调时。
还要考虑重新分解代码,如下所示。 server.listen()
已移至before()
,因此仅it()
内describe()
done()
server.listen()
内的所有it()
执行一次describe('the tcp server', function() {
before(function(done){
server.listen('3000', function(){
console.log('server up in mocha!');
done();
});
});
beforeEach(function(){});
it('should log all requests to a log file', function(done){
request.get('http://localhost:3000').end(function(err, res){
expect(true).to.equal(false);
done();
});
});
});
。这将确保服务器在<div id="output"></div>
执行之前就绪。
var obj = [{
"Name" : "Test1",
"check" : true
}, {
"Name" : "Test2",
"check" : true
}, {
"Name" : "Test3",
"check" : false
}
];
function getNames(){
var length=obj.length;
// alert(length);
var op=[];
for(var i=0;i<obj.length;i++){
// alert(obj[i].Name);
op[i]='"'+obj[i].Name+'"';
}
document.getElementById("output").innerHTML=op;
}
getNames();