我有一个带有Socket.io服务器的Node.js.所以我想用Mocha编写测试来改进开发。我已经搜索了很多关于使用Mocha测试socket.io服务器的信息,我找到的只是http://liamkaufman.com/blog/2012/01/28/testing-socketio-with-mocha-should-and-socketio-client/
但是在Mocha的测试目录下有服务器和测试,两者都是从Mocha开始的,当时我已经有了工作服务器,只想测试它。
所以我写了下一段代码:
var assert = require("assert"),
should = require('should'),
io = require('socket.io-client'),
socketURL = 'http://localhost:3102',
options ={
transports: ['websocket'],
'force new connection': true,
'reconnection delay' : 0,
'reopen delay' : 0
};
describe('Server', function(){
var socket;
beforeEach(function(done) {
// Setup
console.log('Establishing connection');
socket = io.connect(socketURL, options);
socket.on('connect', function() {
console.log('worked...');
done();
});
});
...
it('should receive messages', function(done){
socket.on('message', function(data){
console.log('received');
done();
});
...
})
结果是:
Server
Establishing connection
1) "before each" hook
0 passing (2s)
1 failing
1) Server "before each" hook:
Error: timeout of 2000ms exceeded
at null. (\npm\node_modules\mocha\lib\runnable.js:139:19)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
我检查了服务器的日志,但没有连接......没有。它甚至没有尝试连接到服务器。
所以我想知道是否可以用这种方式用Mocha进行测试?当您拥有完整的工作服务器并尝试测试请求 - 响应功能时。