Improve this doc

The following is a unit test for the 'notify' service in the 'Dependencies' example in Creating Angular Services. The unit test example uses Jasmine spy (mock) instead of a real browser alert.

  1. var mock, notify;
  2.  
  3. beforeEach(function() {
  4. mock = {alert: jasmine.createSpy()};
  5.  
  6. module(function($provide) {
  7. $provide.value('$window', mock);
  8. });
  9.  
  10. inject(function($injector) {
  11. notify = $injector.get('notify');
  12. });
  13. });
  14.  
  15. it('should not alert first two notifications', function() {
  16. notify('one');
  17. notify('two');
  18.  
  19. expect(mock.alert).not.toHaveBeenCalled();
  20. });
  21.  
  22. it('should alert all after third notification', function() {
  23. notify('one');
  24. notify('two');
  25. notify('three');
  26.  
  27. expect(mock.alert).toHaveBeenCalledWith("one\ntwo\nthree");
  28. });
  29.  
  30. it('should clear messages after alert', function() {
  31. notify('one');
  32. notify('two');
  33. notify('third');
  34. notify('more');
  35. notify('two');
  36. notify('third');
  37.  
  38. expect(mock.alert.callCount).toEqual(2);
  39. expect(mock.alert.mostRecentCall.args).toEqual(["more\ntwo\nthird"]);
  40. });

Related Topics

Related API