angular-socket-io-seed icon indicating copy to clipboard operation
angular-socket-io-seed copied to clipboard

testing

Open remkoboschker opened this issue 12 years ago • 10 comments

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

remkoboschker avatar Sep 04 '12 10:09 remkoboschker

Both! Ideally, you want unit tests for all of your controllers, directives, and services, as well as e2e tests.

btford avatar Sep 04 '12 14:09 btford

I'd also be very interested in a testing mock here..

fanktom avatar Mar 06 '13 12:03 fanktom

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.

fanktom avatar Mar 06 '13 15:03 fanktom

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

JavascriptMick avatar Nov 04 '13 09:11 JavascriptMick

@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.

nullivex avatar Nov 12 '13 22:11 nullivex

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 .

JavascriptMick avatar Nov 13 '13 01:11 JavascriptMick

@nullivex nice!

btford avatar Mar 03 '14 00:03 btford

@mdausmann thanks, worked great!

nukulb avatar Jul 14 '14 17:07 nukulb

@nullivex i got undefined on "socketMock = new sockMock($rootScope);"

Could you help me on this ?

anandi2i avatar Mar 14 '16 12:03 anandi2i

@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.

komali2 avatar Jun 11 '16 19:06 komali2