// global
var ws = null;
var msgCount = 0;
var onMessageHandler = null;
// test #1 - this test passes successfully
describe('connect to wsserver test', function() {
it('should successfully connect to wsserver', function(done) {
this.timeout(0);
ws = new FayeWebSocket.Client('wss://server.com', null, {
headers: {
"authToken": "someToken"
}
});
ws.on('open', function() {
done();
});
ws.on('message', function(msg) {
if (onMessageHandler && typeof onMessageHandler === 'function') {
onMessageHandler(msg);
}
});
ws.on('close', function(event) {
console.log('closing websocket!');
});
ws.on('error', function(err) {
console.log("error!: " + err.message);
});
});
});
// test #2 - this test blocks indefinitely
describe('send request and get back 3 response messages', function() {
it('should get back 3 response messages from the wsserver', function(done) {
this.timeout(0);
// this function is called ONLY once, although the server is sending 3 messages
onMessageHandler = function(msg) {
msgCount++;
console.log(msg);
if (msgCount >= 3) {
done();
}
}
var sendThisRequest = {
'some': 'json',
'with': 'some key/value pairs'
}
// this line sends a request to the wsserver
ws.send(JSON.stringify(sendMsg));
});
});
내 websocket api를 테스트하기 위해 몇 가지 기본 단위 테스트를 작성하려고합니다. 이 모카 테스트는 웹 소켓 서버가 아닌 클라이언트를 시뮬레이트합니다.websocket onMessage는 mochajs 테스트에서 한 번만 호출됩니다.
- 첫 번째 테스트에서 websocket을 사용하여 websocket 서버에 연결하면이 테스트가 성공적으로 통과합니다.
- 두 번째 테스트에서 서버에 ws.send (메시지) 요청을 보내면 서버가이 요청을 올바르게 처리하고 일부 처리를 수행하고 3 개의 websocket 메시지을 클라이언트에 보냅니다. (서버 로그를보고,이 부분은 서버에서 잘 작동하고 있다고 말할 수있다.)
- 3 개의 메시지를 얻은 후 테스트가 완료되어야하고 일부는 .to.equal (something) 어설 션이된다.
- I 지금까지 두 번째와 세 번째 메시지가 onMessageHandler()에 의해 선택되지 않은 이유를 지금까지 파악할 수 없었습니다. 나는 ws.on ('message', function (msg) {..}) 블록을 코드의 다른 위치에 놓으려고했으나 아무 소용이 없었다.
누군가가이 문제에 대해 올바른 방향으로 나를 가리킬 수 있다면 좋을 것입니다. 감사합니다.
귀하의 기대 진술은 어디에 있습니까? –
글쎄 지금은 단지 3 개의 메시지를 얻고, expect() 문을 작성하는 것이 나중에 수행 될 수 있도록하려는 것이다. 그것이 문제의 원인이라고 생각하십니까? –
스트리밍 중입니까? –