jeudi 26 mars 2015

How to test $broadcast and $on events in AngularJS

I am trying to test the $broadcast and $on events in Angular. I have a function that creates a date and does some calculation on it, then send the result to another controller via $broadcast.


What I want to be able to do, is test the $broadcast has happened and also the $on listener to see what the result was.


I can test the former but the latter fails.


I have created a JSFiddle with a basic example of what I am trying to achieve here http://ift.tt/1Gruhxx


Works



expect(rootScope.$broadcast).toHaveBeenCalledWith('sendTime',tDate);
});


But....



expect(rootScope.$on).toHaveBeenCalledWith('sendTime', Function);


Gives



Expected spy $on to have been called with [ 'sendTime', Function ] but actual calls were [ 'sendTime', Function ]


which look identical to me!


(for a bonus point, can someone can tell me why my fiddle will not work over https!).


Aucun commentaire:

Enregistrer un commentaire