onmessage
이 적절한 기능인지 테스트 해 봅니다.Jasmine으로 JavaScript에서 WebSocket 스터 빙
FL.init = function(address, window) {
if ('WebSocket' in window) {
var ws = new WebSocket(address);
ws.onmessage = function(e) {
this.handleMessage(e.data);
};
return ws;
}
};
첫 번째 테스트가 통과 : 여기
describe(".init(address, window)", function() {
beforeEach(function() {
address = 'ws://test.address';
window = {};
e = {
data: {}
}
spyOn(window, 'WebSocket').and.returnValue(function() {return {onmessage: null}});
spyOn(subject, 'handleMessage');
});
it("should create a WebSocket client which connects to the given address", function() {
subject.init(address, window);
expect(window.WebSocket).toHaveBeenCalledWith(address);
});
it("should have onmessage method overriden with a function which handles message", function() {
ws = subject.init(address, window);
alert(JSON.stringify(ws));
ws.onmessage(e);
expect(subject.handleMessage).toHaveBeenCalledWith(e.data);
});
});
의 구현 : 여기
테스트이다. 두 번째로
ws
은
undefined
입니다. 왜 그런가요? 콘솔
new function() {return {onmessage: null}}
에서 시도하고 확인해야합니다.