2
我試圖將TDD原理應用於使用node.js和nodeunit進行單元測試的應用程序。我正在努力嘗試創建一個能夠使用websockets測試應用程序通信協議的測試用例。帶網絡套接字的Nodeunit示例
我對TDD相當陌生,所以,這種方法對於TDD是否正確?
你知道我在哪裏可以找到樣品嗎?編輯: 我有一個工作示例。然而,我有問題,服務器需要15秒鐘關閉它自己。我讚賞關於此代碼的反饋(這是WIP)。
文件測試/ websocket.js
var Server = require('../tests/server.js').Server;
var wsServer = new Server();
var ioclient = require("socket.io-client");
var testCase = require('nodeunit').testCase;
exports.server = testCase({
setUp: function(callback) {
wsServer.initialize();
wsServer.addEvent('connection',function(socket){
socket.on('ping',function(data){
socket.emit('ping', { value: 'ok' });
socket.disconnect();
});
});
callback();
},
tearDown: function (callback) {
wsServer.close();
callback();
},
'ping test': function(test) {
var clientsocket = ioclient.connect('http://localhost:' + wsServer.getPort());
clientsocket.on('ping', function(data){
test.equal('ok', data.value);
test.done();
});
clientsocket.emit('ping', { pingRequest: '1' });
},
'another ping test': function(test) {
var clientsocket = ioclient.connect('http://localhost:' + wsServer.getPort());
clientsocket.on('ping', function(data){
test.equal('ok', data.value);
test.done();
});
clientsocket.emit('ping', { pingRequest: '1' });
}
});
文件test/server.js
function Server() {
this.port = 3333;
}
Server.prototype.initialize = function() {
this.create();
}
Server.prototype.getPort= function() {
return this.port;
}
Server.prototype.addEvent = function (name, fn) {
this.instance.sockets.on(name, fn);
}
Server.prototype.create = function() {
var io = require("socket.io").listen(this.port, {'log level': 1});
//io.sockets.on('connection',);
this.instance = io;
}
Server.prototype.close = function() {
this.port ++;
this.instance.server.close();
}
exports.Server = Server;
在這裏發現了一個有趣的鏈接http://nullzzz.blogspot.com/2011/03/socketio-and-asynchronous-testing-with.html – Tk421 2011-12-25 10:25:35