我試圖用nodeunit來測試我的Web服務器:Nodeunit執行順序?
test.js
exports.basic = testCase({
setUp: function (callback) {
this.ws = new WrappedServer();
this.ws.run(PORT);
callback();
},
tearDown: function (callback) {
delete this.ws;
callback();
},
testFoo: function(test) {
var socket = ioClient.connect(URL);
console.log('before client emit')
socket.emit('INIT', 1, 1);
console.log('after client emit');
}
});
,這是我最簡單的服務器的NodeJS:
WrappedServer.prototype.run = function(port) {
this.server = io.listen(port, {'log level': 2});
this.attachCallbacks();
};
WrappedServer.prototype.attachCallbacks = function() {
var ws = this;
ws.server.sockets.on('connection', function(socket) {
ws.attachDebugToSocket(socket);
console.log('socket attaching INIT');
socket.on('INIT', function(userId, roomId) {
// do something here
});
console.log('socket finished attaching INIT');
});
}
基本上我得到這個錯誤:
[...cts/lolol/nodejs/testing](testingServer)$ nodeunit ws.js
info - socket.io started
before client emit
after client emit
info - handshake authorized 1013616781193777373
The "sys" module is now called "util". It should have a similar interface.
socket before attaching INIT
socket finished attaching INIT
info - transport end
不知何故,socket在服務器之前發出INIT爲插座添加回調函數。
這是怎麼發生的?另外,做這件事的正確方法是什麼?
謝謝你的提升。關於「你也應該把setUp的回調傳遞給io.listen」你是什麼意思?至於io.listen並不真正採取回調。即使這樣做,這意味着我將從我的測試文件注入代碼到我的實際服務器? – disappearedng 2012-02-21 00:56:04
更新了我的答案。 – loganfsmyth 2012-02-21 01:09:50