Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing #4

Open
remkoboschker opened this issue Sep 4, 2012 · 10 comments
Open

testing #4

remkoboschker opened this issue Sep 4, 2012 · 10 comments

Comments

@remkoboschker
Copy link

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

@btford
Copy link
Owner

btford commented Sep 4, 2012

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

@fanktom
Copy link

fanktom commented Mar 6, 2013

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

@fanktom
Copy link

fanktom commented Mar 6, 2013

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.

@JavascriptMick
Copy link

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

@nullivex
Copy link

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

@JavascriptMick
Copy link

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//issues/4#issuecomment-28342497
.

@btford
Copy link
Owner

btford commented Mar 3, 2014

@nullivex nice!

@nukulb
Copy link

nukulb commented Jul 14, 2014

@mdausmann thanks, worked great!

@anandi2i
Copy link

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

Could you help me on this ?

@komali2
Copy link

komali2 commented Jun 11, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants