using ngWebSocketMock
lionelB opened this issue · 1 comments
lionelB commented
Hello,
I'm trying to use the ngWebSocketMock
mock for test but it seems that
expectSend
doesn't work well.
expectConnect
works fine once I added the afterEach verifyOutstanding
describe('connect', function(){
beforeEach(function(){
$websocketBackend.mock();
$websocketBackend.expectConnect('ws://websocket.url:443');
});
afterEach(function(){
$websocketBackend.verifyNoOutstandingExpectation();
$websocketBackend.verifyNoOutstandingRequest();
});
it('should connect to a websocket', function() {
var socket = $websocket('ws://websocket.url:443');
$websocketBackend.flush();
});
});
But I can't figure how use expectSend()
I fix an error here since _onMessageHandler is called with a MessageEvent
- this._onMessageHandler($websocketBackend.mockSend());
+ this._onMessageHandler({data: $websocketBackend.mockSend()});
Can you provide a working (passing/failing) test case example ?
lionelB commented
after digging inside the mock library, I found that update that the problem come from here
- if (pending.message === msg.message) {
+ if (pending === msg) {
and this a my working / failin test case
describe('publish', function() {
var socket;
beforeEach(function(){
$websocketBackend.expectConnect('ws://websocket.url:443');
$websocketBackend.expectSend( JSON.stringify({msg:'yolo'}) );
socket = $websocket('ws://websocket.url:443');
// Fake socket connection open
socket.socket.readyState = 1;
})
it('should publish data', function(){
socket.send( JSON.stringify({msg:'yolo'}) );
$websocketBackend.flush();
})
});
Hope it helps
I will try to submit a PR for the mock library