I have package for creating Websocket connections (rather simplified version of what I am trying to test):
(function() {
window.ConnectionFactory.connect = function (url, onConnect) {
this.ws = new WebSocket(url);
this.ws.onopen = function () {
// do some stuff
onConnect(this.ws);
};
}
}).call(this);
I want to test two things in this function
- The web socket is created with the given url.
- The on connect callback is invoke.
So I wrote this test
describe('Test Connect', function() {
describe('Successfull connection', function () {
beforeEach(function () {
function MockWebSocket(url) {
url = url;
setTimeout(this.onopen, 0);
}
this.ws = spyOn(window, 'WebSocket').and.callFake(MockWebSocket);
this.onConnectSpy = jasmine.createSpy();
ConnectionFactory.connect(
'url',
this.onConnectSpy
);
});
it('Should create websocket with the given url', function () {
expect(this.ws).toHaveBeenCalled();
expect(this.ws.calls.argsFor[0]).toEqual('url');
});
it('Should call onConnect callback', function () {
expect(this.onConnectSpy).toHaveBeenCalled();
});
});
});
Both test fail and i'm a bit lost. Appreciate any help with this. Thanks.
Aucun commentaire:
Enregistrer un commentaire