我正在尝试测试一个使用WebSockets的库。 我正试图使用以下代码模拟websocket。库ROSController
使用websockets,但我不断收到WebSocket未定义
的错误。
import { ROSController } from '../ROSController.jsx';
var socketMock;
var windowMock;
var address = 'ws://test.address';
beforeAll(function() {
var WebSocket = jasmine.createSpy();
WebSocket.and.callFake(function (url) {
socketMock = {
url: url,
readyState: WebSocket.CONNECTING,
send: jasmine.createSpy(),
close: jasmine.createSpy().and.callFake(function () {
socketMock.readyState = WebSocket.CLOSING;
}),
// methods to mock the internal behaviour of the real WebSocket
_open: function () {
socketMock.readyState = WebSocket.OPEN;
socketMock.onopen && socketMock.onopen();
},
_message: function (msg) {
socketMock.onmessage && socketMock.onmessage({data: msg});
},
_error: function () {
socketMock.readyState = WebSocket.CLOSED;
socketMock.onerror && socketMock.onerror();
},
_close: function () {
socketMock.readyState = WebSocket.CLOSED;
socketMock.onclose && socketMock.onclose();
}
};
return socketMock;
});
WebSocket.CONNECTING = 0;
WebSocket.OPEN = 1;
WebSocket.CLOSING = 2;
WebSocket.CLOSED = 3;
windowMock = {
WebSocket: WebSocket
};
return WebSocket;
});
test('the subscription JSON produced is correct', () => {
console.log(WebSocket); //<----- It fails here
JSON.parse((new ROSController('')).callService('/test','', function(){}));
});
jasmine
声明在哪里? - jered