angular-socket-io-seed
angular-socket-io-seed copied to clipboard
testing
Hi,
Nice tutorials, thanks.
How would you go about writing tests for a setup like this one?
mock socket.io server connection in the unit tests in the angular client and vice versa?
angular e2e tests for client and server together?
Regards,
Remko
Both! Ideally, you want unit tests for all of your controllers, directives, and services, as well as e2e tests.
I'd also be very interested in a testing mock here..
I wrote a test mock myself, maybe it is interesting for you:
appMock.service("socket", function($rootScope){
this.events = {};
// Receive Events
this.on = function(eventName, callback){
if(!this.events[eventName]) this.events[eventName] = [];
this.events[eventName].push(callback);
}
// Send Events
this.emit = function(eventName, data, emitCallback){
if(this.events[eventName]){
angular.forEach(this.events[eventName], function(callback){
$rootScope.$apply(function() {
callback(data);
});
});
};
if(emitCallback) emitCallback();
}
});
It allows to setup listener functions and on emit they simply all get called back with the data emitted. Testing can by done synchronously like this:
it("emits and receives messages", function(){
var testReceived = false;
socket.on("test", function(data){
testReceived = true;
});
socket.emit("test", { info: "test" });
expect(testReceived).toBe(true);
});
Simply load the mock module after the main module, that will overwrite the previous socket service.
Hi Guys
I have taken SouthDesign's example above.. Tweaked it a bit and incorporated it into a Karma test suite for my controller. You can check out my controller test here...
https://github.com/hackify/hackify-server/blob/master/test/controllers.test.js
Pull the whole thing if you like and follow the readme instructions to run the Karma tests.
I found that I needed to add a Receive event to differentiate the mock emits and the emits I expect from the controller.
@btford happy to do a pr for this, not super sure how to approach it tho.
Michael
@mdausmann I created a package using your mock object that can be downloaded using bower which is located here: https://github.com/nullivex/angular-socket.io-mock
This is working well for me so far and I am testing with Mocha, Chai and using RequireJS. Thought you might find it useful.
I load it as a drop in replacement for the angular-socket-io package and it works like a charm.
Fantastic Bryan I will check it out On 13/11/2013 9:57 AM, "Bryan Tong" [email protected] wrote:
@mdausmann https://github.com/mdausmann I created a package using your mock object that can be downloaded using bower which is located here: https://github.com/nullivex/angular-socket.io-mock
This is working well for me so far and I am testing with Mocha, Chai and using RequireJS. Thought you might find it useful.
I load it as a drop in replacement for the angular-socket-io package and it works like a charm.
— Reply to this email directly or view it on GitHubhttps://github.com/btford/angular-socket-io-seed/issues/4#issuecomment-28342497 .
@nullivex nice!
@mdausmann thanks, worked great!
@nullivex i got undefined on "socketMock = new sockMock($rootScope);"
Could you help me on this ?
@anandi2i not sure but try
appMock.service("sockMock", function($rootScope){
this.events = {};
// Receive Events
this.on = function(eventName, callback){
if(!this.events[eventName]) this.events[eventName] = [];
this.events[eventName].push(callback);
}
// Send Events
this.emit = function(eventName, data, emitCallback){
if(this.events[eventName]){
angular.forEach(this.events[eventName], function(callback){
$rootScope.$apply(function() {
callback(data);
});
});
};
if(emitCallback) emitCallback();
}
});
If that doesn't work, look through his repo to find where he defined the class sockMock.