title: angularjs + jasmine : testing focus in a service
body: <p>Learning jasmine for the first time and I am stuck on this error when trying to test the focus() functionality in an angular service. 
  Here is the service:</p>
  
  <pre><code>myApp.service('MyService', function($timeout, $window) {
    var service = {
      focusElem: focusElem
    };
    return service;
  
    function focusElem(id) {
      console.log('id of element is = ', id);
      if (id) {
  
        $timeout(function() {
          var element = $window.document.getElementById(id);
          console.log('element is = ', element);
          if (element) {
            element.focus();
          }
        });
      }
    };
  
  });
  </code></pre>
  
  <p>Here is my spec file </p>
  
  <pre><code>describe('myApp', function() {
    var element, dummyElement;
    beforeEach(function() {
      // Initialize myApp injector
      module('myApp');
  
      // Inject instance of service under test    
      inject(function($injector) {
        MyServiceObj = $injector.get('MyService');
      });
  
      element = angular.element('&lt;input id="firstName" name="firstName"/&gt;');
      dummyElement = document.createElement('input');
      dummyElement.setAttribute('id', 'lastName');
  
    });
  
    it('should have focus if the focus Service is used on an element', function() {
      console.info('------------------');
      spyOn(element[0], 'focus');
      spyOn(dummyElement, 'focus');
      MyServiceObj.focusElem(dummyElement.getAttribute('id'));
      expect(dummyElement.focus).toHaveBeenCalled();
    });
  });
  </code></pre>
  
  <p>My error: </p>
  
  <pre><code>myApp should have focus if the focus Service is used on an element
  Expected spy focus to have been called.
  Error: Expected spy focus to have been called.
  </code></pre>